Search Unity

Question Translating player movement to animator floats

Discussion in 'Scripting' started by glacierqsr, Feb 14, 2024.

  1. glacierqsr

    glacierqsr

    Joined:
    Nov 9, 2021
    Posts:
    11
    Hey there, I've been messing around with animation blend trees and after a while I've finally got the animations set up.
    Now I am faced with a new problem, I'm not sure how to convert my players movement into the animator floats.
    Code (CSharp):
    1.  anim.SetFloat("SpeedX", controller.moveDirection.x);
    2.  anim.SetFloat("SpeedZ", controller.moveDirection.z);
    This is what I've currently got set up for changing the movement values into the animator values, but this obviously isn't working.
    Code (CSharp):
    1. private void ApplyFinalMovements()
    2. {
    3.     if (!ctrl.isGrounded)
    4.         moveDirection.y -= gravity * Time.deltaTime;
    5.     ctrl.Move(moveDirection * Time.deltaTime);
    6. }
    7.  
    8. private void HandleMovementInput()
    9. {
    10.     currentInput = new Vector2((IsSprinting ? sprintSpeed : walkSpeed) * Input.GetAxis("Vertical"), (IsSprinting ? sprintSpeed : walkSpeed) * Input.GetAxis("Horizontal"));
    11.     float moveDirectionY = moveDirection.y;
    12.     moveDirection = (transform.TransformDirection(Vector3.forward) * currentInput.x) + (transform.TransformDirection(Vector3.right) * currentInput.y);
    13.     moveDirection.y = moveDirectionY;
    14. }
    This is the code I'm using to move the player, and as you can see I'm not using the "new" input system.
    So now my question is how can I translate the players movement into the animators parameter floats?
     
  2. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    Well... what are you having trouble with?

    You've got a Vector3 there. You can call SetFloat(...) on the Animator and pass your X and Y values in. We've no idea what anything looks like, how it moves, etc. so can't be more detailed than that, but it'll be something like you already have on line 12.

    The details will depend on your character and movement design, which we don't know anything about. Description and diagrams / screenies would help.

    Is it top down, side on, first person..?

    What is happening? How is it different to what you want to happen?
     
  3. glacierqsr

    glacierqsr

    Joined:
    Nov 9, 2021
    Posts:
    11
    Hey there, thanks for taking the time to reply.
    The second part of the reply has made me realize that I will need to update a few things(mainly the camera) to get the controller working fully in the first place, sorry for wasting your time with the post.

    I'll post another(more detailed) thread once I've fixed the issues with the camera(I have a 3rd person camera rotating the player independently of the movement, which makes the turning on the animations really wonky)

    ps: ill make sure to read the link under your reply ;)
     
    angrypenguin likes this.