Search Unity

Set Animator Parameter based on move direction?

Discussion in 'Scripting' started by RobertOne, Feb 1, 2021.

  1. RobertOne

    RobertOne

    Joined:
    Feb 5, 2014
    Posts:
    259
    Hey i am working on a twin stick top down game.
    left stick: move - left right up down.
    right stick: look in the direction the stick gets dragged

    works all fine but now i wanna combine that with some animations.
    so when i drag the left stick to the left and the right stick up i want in the animator to be:
    Horizontal: -1 Vertical: 0
    so i can play a walk sideward animation

    but when i drag the left stick to the left and the right stick to the left too i want in the animator like:
    Horizontal: 0 Vertical: 1
    so i can play a walk forward animation

    how can i do that?
    here is my code so far for the movement:

    Code (CSharp):
    1.  
    2.  
    3.         var _camRel = _rightStickInput.y * mainCamera.transform.forward + _rightStickInput.x * mainCamera.transform.right;
    4.         var axisDirection = _camRel + transform.position;
    5.         _lookAtTarget = new Vector3(axisDirection.x, transform.position.y, axisDirection.z);
    6.  
    7.         transform.LookAt(_lookAtTarget, transform.up);
    8.         var controlDirection = new Vector3(_leftStickInput.x, 0, _leftStickInput.y);
    9.         var actualDirection = mainCamera.transform.TransformDirection(controlDirection);
    10.  
    11.         _animator.SetFloat("Horizontal", controlDirection.x);
    12.         _animator.SetFloat("Vertical", controlDirection.y);
    13.