public void getNextPosition()
{
float normalizedStep = 8.5f;
float step = normalizedStep * Time.deltaTime;
// Init nextPos to the current position
Vector3 nextPos = new Vector3(transform.position.x, transform.position.y, transform.position.z);
// Determine X and Y value for next position
Vector3 directionVect = (dest - nextPos).normalized;
float angle = Vector3.Angle(directionVect, Vector3.right)/360*2*Mathf.PI;
if (Vector3.Angle (directionVect, Vector3.forward) > 90f)
angle = -angle;
nextPos.x = nextPos.x + step * Mathf.Cos(angle);
nextPos.y = parabolaXYCoef.getValueFor(nextPos.x);
nextPos.z = nextPos.z + step * Mathf.Sin(angle);
/*
// Validate the Y value, to have at least one result for the Z component below.
float vertY = parabolaZYCoef.getVertex().y;
bool isAPositive = parabolaXYCoef.isAPositive();
if ((isAPositive && vertY > nextPos.y) || (!isAPositive && vertY < nextPos.y))
{
nextPos.y = parabolaZYCoef.getVertex().y;
}
// Determine the Z value for the next position
List<float> res = parabolaZYCoef.getArgOf(nextPos.y);
if (res.Count == 2) {
nextPos.z = res[0];
Vector3 vec1 = nextPos;
nextPos.z = res[1];
Vector3 vec2 = nextPos;
// If we are far of the top vertex, choose the nearest vector
if ((vec1 - vec2).magnitude > 2*step)
{
if ((transform.position - vec1).magnitude < (transform.position - vec2).magnitude)
nextPos.z = res[0];
else
nextPos.z = res[1];
}
// Else, if we are close to the top vertex
else {
// Determine the result that give to (nextPos - transform.position) a similar direction with velocity.
float dot0 = Vector3.Angle(vec1 - transform.position, velocity);
float dot1 = Vector3.Angle(vec2 - transform.position, velocity);
if (dot0 < dot1)
nextPos.z = res[0];
else
nextPos.z = res[1];
}
}
else if (res.Count == 1) {
nextPos.z = res[0];
}
else {
throw new System.Exception("CameraPath.getNextPosition: Invalid result of parabolaZYCoef.getArgOf.");
}*/
// Update the current position
//if ((nextPos - transform.position).magnitude > (dest - transform.position).magnitude)
if ( Mathf.Sign(nextPos.x - dest.x) != Mathf.Sign(transform.position.x - dest.x) )
{
velocity = Vector3.zero;
transform.position = nextPos = dest;
}
else
{
velocity = (nextPos - transform.position)/Time.deltaTime;
transform.position = nextPos;
}
}