Search Unity

Calculating nav mesh agent turning animations

Discussion in 'Scripting' started by pizzaboy13, Apr 2, 2020.

  1. pizzaboy13

    pizzaboy13

    Joined:
    Mar 6, 2019
    Posts:
    18
    I have 4 animations, turning left or right 90 degrees and 180 degrees. I have a nav mesh agent and a position in world space I want to set their destination to. How do I calculate which animation to use, according to how much the agent would have to turn to face the target?
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    Code (csharp):
    1. Vector3 toTarget = destination - agent.transform.position;
    2. toTarget.y = 0;
    3.  
    4. var angle = Vector3.Angle(agent.transform.forward, toTarget)
     
    pizzaboy13 likes this.
  3. pizzaboy13

    pizzaboy13

    Joined:
    Mar 6, 2019
    Posts:
    18

    This works great except I can't distinguish whether or not to the agent is turning right or left
     
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    pizzaboy13 likes this.
  5. pizzaboy13

    pizzaboy13

    Joined:
    Mar 6, 2019
    Posts:
    18
    I used SignedAngle instead of Angle with the agents transform.up as the axis and it viola, it works perfectly now. Thank you!