📜  unity 用动画移动角色 - C# 代码示例

📅  最后修改于: 2022-03-11 14:48:53.942000             🧑  作者: Mango

代码示例1
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{

    public CharacterController controller;
    public Animator animator;

    public float speed = 6f;

    // Start is called before the first frame update
    void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
    }

    // Update is called once per frame
    void Update()
    {
        Vector3 moveForwards = new Vector3(0f, 0f, 1f);
        Vector3 Stop = new Vector3(0f, 0f, 0f);

        if (Input.GetKey(KeyCode.W))
        {
            animator.SetInteger("Condition", 1);

            controller.Move(moveForwards * speed * Time.deltaTime);
        }

        if (Input.GetKeyUp(KeyCode.W))
        {
            animator.SetInteger("Condition", 0);

            controller.Move(Stop);
        }
    }
}