Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Need help implementing GTA V style walking animation system

Discussion in 'Animation' started by Gameatro, Apr 22, 2020.

  1. Gameatro

    Gameatro

    Joined:
    Sep 15, 2017
    Posts:
    9
    I am trying to implement GTA V style walking system, as in: When the character is facing the same direction as camera, and we press D, it first turns by quarter and if the key is kept pressed, character starts walking in right. Similarly if S is pressed, it turns half and starts walking back towards camera. And the animation played depends on current character position wrt camera, eg. if the character is facing right and A is pressed, it turns half left and then if kept pressed, starts walking.
    I am trying to implement this in Unity. But I am having trouble finding the angle between camera and player in the Y plane. I tried using ProjectOnPlane function but I am getting all wrong angles. Here is my code for pressing of D key:
    Code (CSharp):
    1.  
    2. void Right()
    3.     {
    4.         Vector3 pVec = Vector3.ProjectOnPlane(transform.forward, Vector3.up);
    5.         Vector3 cVec = Vector3.ProjectOnPlane(mainCam.transform.forward, Vector3.up);
    6.         print(cVec);
    7.         float angle = Vector3.Angle(pVec, cVec);
    8.         print(angle);
    9.         if(angle >= 345 && angle <= 15)
    10.         {
    11.             animator.Play("StandQuarterTurnRight");
    12.         }
    13.         else if(angle >= 255 && angle <= 285)
    14.         {
    15.             animator.Play("StandHalfTurnRight");
    16.         }
    17.         else if(angle >= 165 && angle <= 195)
    18.         {
    19.             animator.Play("StandQuarterTurnLeft");
    20.         }
    21.         else if(angle >=75 && angle <= 105)
    22.         {
    23.             float forw = Input.GetAxis("Horizontal");
    24.             if (forw > 0.5f && !Input.GetKey(KeyCode.LeftShift)) forw = 0.5f;
    25.             else if (forw < -0.5f && !Input.GetKey(KeyCode.LeftShift)) forw = -0.5f;
    26.             animator.SetFloat("Speed", forw);
    27.         }
    28.     }
    But I am getting wrong angles, I get 90 and nearby when my character is facing left as well as right side of the camera and I don't get angles greater than 180. What am I doing wrong? Is there any better way to implement this?
     
  2. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,936
    You can install the Unity standard asset characters to see how it's done:

    Assets -> Import Package -> Characters
     
  3. Gameatro

    Gameatro

    Joined:
    Sep 15, 2017
    Posts:
    9
    How will installing standard asset help solve my problem? Also, I have imported them already, I am using the animations from standard asset. I am having problem with the angles.