Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

Matching blend tree animation direction in top-down fixed camera game

Discussion in 'Animation' started by ApocFI, Nov 16, 2018.

  1. ApocFI

    ApocFI

    Joined:
    May 26, 2017
    Posts:
    33
    I have a problem with my top perspective shooter game. I have the camera following the player around the scene, and the player is looking/aiming at mouse cursor. I'm using a blend tree animation (using 2d freeform cartesian blend type) which picks the animation based on Horizontal and Vertical float values set in movement script.

    This leads to animations playing in a fixed way too. It's ok if the player is aiming directly up, but once the player turns the animations are instantly off. Since the movement is fixed to be relative to the camera (basic WASD), and if the player turns around and aims at the bottom of the screen, then inputs to move left (on the screen) by pressing A, the animation receives left input too - Although in local space the character is moving to the right.

    So what I need to do is somehow translate this setFloat-value to be always relative to the player character, but I'm having really hard time figuring it out. So far I've found couple of similar threads, especially this: https://forum.unity.com/threads/problems-with-backward-waliking-animation-in-blend-tree.425126/
    That script is based on exactly the same one I'm using, but I couldn't make it work with the given advice. The setFloat values were still the same fixed ones, plus in range of 0-2 instead of 0-1. How do I solve this?

    Here's the movement script:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class PlayerMovement : MonoBehaviour
    4. {
    5.     public float speed = 4f;
    6.     public float m_Damping = 0.15f;
    7.  
    8.     Vector3 movement;
    9.     Animator anim;
    10.     Rigidbody playerRigidBody;
    11.     int floorMask;
    12.     float camRayLength = 100f;
    13.     Camera aimCam;
    14.  
    15.     private readonly int m_HashHorizontalPara = Animator.StringToHash("Horizontal");
    16.     private readonly int m_HashVerticalPara = Animator.StringToHash("Vertical");
    17.  
    18.  
    19.     void Awake ()
    20.     {
    21.         floorMask = LayerMask.GetMask ("Floor");
    22.         anim = GetComponent <Animator> ();
    23.         playerRigidBody = GetComponent <Rigidbody> ();
    24.         aimCam = GameObject.FindGameObjectWithTag("AimCamera").GetComponent<Camera>();
    25.  
    26.     }
    27.  
    28.     void FixedUpdate ()
    29.     {
    30.         //store inputs
    31.         float h = Input.GetAxisRaw("Horizontal");
    32.         float v = Input.GetAxisRaw("Vertical");
    33.  
    34.         //normalize the inputs. Remove
    35.         Vector2 input = new Vector2(h, v).normalized;
    36.  
    37.         //set the horizontal and vertical float values for the animation blend tree
    38.         anim.SetFloat(m_HashHorizontalPara, input.x, m_Damping, Time.deltaTime);
    39.         anim.SetFloat(m_HashVerticalPara, input.y, m_Damping, Time.deltaTime);
    40.  
    41.         Move(h, v);
    42.         Turning ();
    43.         Animating (h, v);
    44.     }
    45.  
    46.     void Move (float h, float v)
    47.     {
    48.         movement.Set (h, 0f, v);
    49. // normalization might not be needed anymore as it is done previously
    50.         movement = movement.normalized * speed * Time.deltaTime;
    51.  
    52.         playerRigidBody.MovePosition (transform.position + movement);
    53.     }
    54.  
    55.     void Turning ()
    56.     {
    57.         Ray camRay = aimCam.ScreenPointToRay(Input.mousePosition);
    58.  
    59.         RaycastHit floorHit;
    60.  
    61.         if (Physics.Raycast (camRay, out floorHit, camRayLength, floorMask))
    62.         {
    63.             Vector3 playerToMouse = floorHit.point - transform.position;
    64.             playerToMouse.y = 0f;
    65.  
    66.             Quaternion newRotation = Quaternion.LookRotation (playerToMouse);
    67.             playerRigidBody.MoveRotation (newRotation);
    68.  
    69.         }
    70.     }
    71.  
    72.     void Animating(float h, float v)
    73.     {
    74.         bool walking = h != 0f || v != 0f;
    75.         anim.SetBool ("IsWalking", walking);
    76.     }
    77. }
     
unityunity