Mach animation with WASD move system

Hey! I am trying to use the WASD to control the Player, and I meet a problem. I do not know how to match the animation with the move, and do not know how to get the local speed of the player. I did come up a way to make the animation works, but definitely not a good way. Can anybody teach me a better method about this?

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class Mover : MonoBehaviour

{

public CharacterController player;

public Transform mainCamera;

public float playerMoveSpeed = 6f;

public float playertrunSnoothTime = 0.1f;

float playertrunSmoothVelocity;

private void Update()

{



   MovePlayer();

        

   UpdateAnimator();

}

public void MovePlayer()

{

    float playerMoveHorizontal = Input.GetAxisRaw("Horizontal");

    float playerMoveVertical = Input.GetAxisRaw("Vertical");

    Vector3 playerdirection = new Vector3(playerMoveHorizontal, 0, playerMoveVertical).normalized;

    if (playerdirection.magnitude >= 0.1f) 

    {

        float N_targetAngle = Mathf.Atan2(playerdirection.x, playerdirection.z) * Mathf.Rad2Deg+mainCamera.eulerAngles.y;

        float N_angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, N_targetAngle, ref playertrunSmoothVelocity, playertrunSnoothTime);

        transform.rotation = Quaternion.Euler(0f, N_angle, 0f);

        Vector3 N_moveDir = Quaternion.Euler(0f, N_targetAngle, 0f) * Vector3.forward;

        player.Move(N_moveDir.normalized * playerMoveSpeed * Time.deltaTime);

        

    }

 

}

protected void UpdateAnimator()

{

    if(Input.GetAxis("Horizontal")!=0 ||Input.GetAxis("Vertical")!=0)

    {

    Vector3 velocity = GetComponent<CharacterController>().velocity; 

    Vector3 localVelocity = transform.InverseTransformDirection(velocity);

    float speed = localVelocity.z; 

     GetComponent<Animator>().SetFloat("ForwardSpeed",speed); 

    }

    else

    {

         GetComponent<Animator>().SetFloat("ForwardSpeed",0); 

    }    

   

}

}

2 Likes

The short answer is lots and lots of experimentation. It appears you’re using the old Character Controller from the Standard Assets.
The speed you’re looking for to send to the animator is how many meters the character will move in one second. That happens to be playerMoveSpeed, and may be the best place to start.

A quick word of warning, the course is centered around a click to move/navmesh based movement system, which extends itself into target selection, and later dialogues, shops, etc. There will be challenging issues going forward that a WASD system will conflict with.

By no means, am I saying you can’t move in this direction, and several students have done so, but it does present issues, and we won’t have an answer for all of them. My general suggestion for students is to go through the course materials once using the click to move scheme so that you understand what’s going on under the hood, and then use what you can port into a WASD scheme. I always recommend having a version of the project as close as possible to the course project.

2 Likes

Thank you very much! I will keep that in mind! I just reinforce my early course so come up with this question: )

2 Likes

Privacy & Terms