Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Move character once hes facing the direction of travel

Discussion in 'Scripting' started by TheCelt, May 2, 2016.

  1. TheCelt

    TheCelt

    Joined:
    Feb 27, 2013
    Posts:
    741
    Hello

    I have a script which turns and moves the character in the direction of travel like this:


    Code (CSharp):
    1. animation.SetFloat("Forward",0.2f);
    2.  
    3. Vector3 direction = (target - transform.position).normalized;
    4. Vector3 newDir = Vector3.RotateTowards(transform.forward, direction, 3.0f * Time.deltaTime, 0.0F);
    5.  
    6. transform.rotation = Quaternion.LookRotation(newDir);
    7. transform.Translate(direction * Time.deltaTime * walkSpeed, Space.World);
    But i don't want the character to walk forwards until he is facing the desired direction, otherwise he is walking and turning at same time and it doesn't look right.

    How can i check the angle of the character's direction against travel direction ? Also is there a simple way to know if the turning angle is left or right (this would help so i can add a turning animation in future).
     
  2. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
  3. TheCelt

    TheCelt

    Joined:
    Feb 27, 2013
    Posts:
    741
    Thats not what i need. I need to know the angle of the chracter to the vector3 target.