Use rigid bodies and animation state machines to complete simple player movements

Players need to have rigid body components, Animator animation state machines, and Colliders.

Player code:

public class NewPlayer : MonoBehaviour
{
    private Animator ani;
    private Rigidbody r;
    private AnimatorStateInfo BC;
    float h, v;
    float speed = 3;
    void Start()
    {
        r= GetComponent<Rigidbody>();
        ani = GetComponent<Animator>();
    }

    void Update()
    {
        h= Input.GetAxis("Horizontal");
        v = Input.GetAxis("Vertical");
        Vector3 dir = new Vector3(h,0,v);
        r.MovePosition(transform.position + dir * Time.deltaTime * speed);
    }
    private void LateUpdate()
    {
        ani.SetFloat("H", h);
        ani.SetFloat("V", v);
    }
}

Animation state machine:

 Create a New Bleed Tree state tree, two Float variables H, V

 

 Players will judge based on the value pressed by the player and select the corresponding animation.

    private void LateUpdate()
    {
        ani.SetFloat("H", h);
        ani.SetFloat("V", v);
    }

 

Guess you like

Origin blog.csdn.net/m0_71624363/article/details/130208875