- Home /
Which direction to rotate a vector for desired velocity?
I have a follower object that moves towards the cursor. I get the desired heading by subtracting the position of the leader (mouse cursor) from the followers position. Movement happens in the X and Z, it rotates about the Y axis.
Vector3 desiredVel = target.transform.position - transform.position;
I also am doing a bunch of collision avoidance and stuff that isn't relevant here.
I want the follower to turn in large arcs, so as not to turn in place. I am achieving this by detecting the angle between the current velocity vector and the desired velocity vector. If it is greater than 90, I move forward and adjust the angle of the velocity vector by a small amount. However, the direction I rotate the vector is always the same. See here:
float angle = Vector3.Angle(steering, velocity);
if (angle > 90) //turn around
{
Vector3 rotatedVector = Quaternion.AngleAxis(1, Vector3.up) * velocity; //rotated velocity
velocity = Vector3.Lerp(velocity, rotatedVector, .1f); //lerping the vector to ensure no quick turns
}
else
{
//seek (not relevant here)
}
The problem is that rotating it by 1 means rotating it clockwise, always. And, the angle from Vector3.Angle()
is always positive, between 0 and 180. Thus far, my solution is to grab the heading again and perhaps check the X and Z components of that vector, but I am unsure if that is reliable
How can I determine which way to rotate my velocity? Put another way: How can I determine whether or not to use a positive or negative rotation on my velocity?