Search Unity

Triggering the right animation when climbing downhill/uphill

Discussion in 'Animation' started by OlafsOsh, Mar 12, 2019.

  1. OlafsOsh

    OlafsOsh

    Joined:
    Jul 10, 2018
    Posts:
    61
    Hey!


    How to make Animator Controller to understand, when the character is climbing uphill and/or downhill?

    I understand, that one way to go would be to some sort trigger collider, which switches in/off the animation needed, but that seems a bit butchered way to do things, as that would mean to put those gazillion colliders all over terrain.

    I PRESUME there is away to determine, when the character ascending or descending more like "this amount-of-Yunits per Xaxis distnce", so it works wherever Character climbs something steeper than normal. Basically to track the overall Y movement in the last couple frames and adjust the animation based on this.

    Or something similar.

    Maybe you know some tutorials?

    Somehow I made my way towards this code, but I still cannot figure out, how to calculate it.
    P.S. There are comments "//ELEVATION" on the side of the lines I added to movement script.


    Code (CSharp):
    1. public class Movement : MonoBehaviour {
    2.  
    3.     public CharacterController2D controller;
    4.     public Animator animator;
    5.     public float horizontalMove = 0f;
    6.     public float elevation = 0f; /// ELEVATION
    7.     public float speed = 300f;
    8.     new Rigidbody2D rigidbody;
    9.  
    10.     // Use this for initialization
    11.     void Start () {
    12.         //rigidbody = GetComponent<Rigidbody2D>();
    13.     }
    14.    
    15.     // Update is called once per frame
    16.     void Update ()
    17.     {
    18.        horizontalMove = Input.GetAxisRaw("Horizontal") * speed;
    19.         elevation =   ; // ELEVATION
    20.         animator.SetFloat("Speed", Mathf.Abs(horizontalMove));
    21.         animator.SetFloat("ElevationTrigger", elevation); // ELEVATION
    22.  
    23.     }
    24.     private void FixedUpdate()
    25.     {
    26.         //rigidbody.MovePosition(rigidbody.position * horizontalMove * speed * Time.fixedDeltaTime);
    27.         controller.Move(horizontalMove * Time.fixedDeltaTime, false, false);
    28.  
    29.     }
    30. }