# Turning animation depending on the angle between current direction and new direction

Hello guys,

I have been trying to achieve this for a while now without results.

Situation: I have a NPC fish. The fish has 3 animations, 1 swim forwards, 2 swim left, 3 swim right. What I need to achieve is for the fish to use a percent of the turn animations depending on the difference between the current forward direction of the fish and the new direction the fish is going to face.

I would appreciate any help.

Regards,
Carlos

Is this a sprite animation or a 3D animation?

``````float dotResult = Vector3.Dot(Vector3 currentForward, Vector3 newForward);
``````

dotResult will be a number from -1 to 1.
-1 is when the new direction is exactly the opposite of the current direction.
0 is when the two directions are exactly perpendicular to each other.
1 is if new direction and exactly the same as current direction

if you want to convert a degree threshold from degrees to a dot value then you can simply use ArcCosine.
say for example you want a threshold of 30 degrees (use new animation if new directions is more than 30 degrees from current direction

``````var angleThreshold = 30;

var angleClamped = Mathf.PingPong(angleThreshold,180);
var dotThreshold = Mathf.Acos(angleClamped * Mathf.Deg2Rad);

if(Vector3.Dot(currentForward,newForward)<dotThreshold)
{
// new forward is more than [angleThreshold] degrees from current forward
}
``````

Hello JoshuaMcKenzie,

Thanks for your reply. I figured out finally the simplest solution for what I wanted to achieve and I am posting it here just in case someone else want to do the same:

Requierement: gameObject NPC that has at least three moving animations, one forward, one left, one right. The NPC must have a Animator with one Blend tree using the â€ś2D Freeform Cartesianâ€ť Blend Type. the three animation should be in this order: Left, Forward, Right. In the X POS column enter: -1 for Left, 0 for Forward, 1 for Right. Two float parameters are needed: you can name them whatever you want of course. The code only uses one of the parameters, the other is there in order to get the -1.0 value in the first parameter which is the only used in the code. Now in the Blend Tree parameters fields, add the parameter you are going to use in the code in the first slot, and in the second slot the other float parameter(we wonâ€™t use that one but it needs to be there).

Add the code below to a class that handles the movement of the NPC and it is in the GameObject with the animator on it:

Here is the very small code to get the animations to smoothly go from one state to another depending on a â€śgoalâ€ť that the NPC is going to. If the goal is to the right of the NPC, the animation state will change from forward to right depending on how far to the right the goal is, for example, and so forth:

``````//At the top of the class
public Animator animatorVariable;
Vector3 goalRelPos = Vector3.zero;
int TurningParam = Animator.StringToHash("Your parameter name here");//this is just to catch the float as a variable, this is faster based on Unity documents

//Call this method from Update
void LeftOrRight()
{
goalRelPos = myTrans.InverseTransformPoint(goalPos).normalized;
animatorVariable.SetFloat(TurningParam, goalRelPos.x);
}
``````

I hope this helps someone.

Regards,
Carlos

1 Like