unity3d Unity 3D,动画不会从Bool上的空闲状态切换

kwvwclae  于 2022-11-24  发布在  其他
关注(0)|答案(1)|浏览(145)

我知道我的代码是不正确的,我试图简单地添加一个动画师字段,重点是什么时候从空闲状态下从事下一个动画。
我对我的AWSD控件很满意,但是我在网上找到的其他代码都使用了不同的控件布局,我只是对如何在别人的代码上添加私有条目感到困惑。伙计们,我是Unity的新手,也是编码的新手。
请帮帮忙!!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{

    private float speed = 5.0f;

    private Animator animator;

    // Start is called before the first frame update
    void Start()
    {
        animator = GetComponent<Animator>();
        speed = 5.0f;
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKey(KeyCode.W))
        {
            transform.Translate(Vector3.forward * Time.deltaTime * speed);
        }
        if (Input.GetKey(KeyCode.S))
        { 
            transform.Translate(-1 * Vector3.forward * Time.deltaTime * speed);
        }
        if (Input.GetKey(KeyCode.A)) { 
            transform.Rotate(0, -1, 0);
        }
        if (Input.GetKey(KeyCode.D))
        {
            transform.Rotate(0, 1, 0);
    }
        `animator.SetBool("IsMoving", true);
        {
            animator.SetBool("IsMoving", false);
        }`
  
    }
}



The reference I am using is this video from youtube  https://www.youtube.com/watch?v=2_Hn5ZsUIXM&t=552s
qjp7pelc

qjp7pelc1#

您可以使用临时bool变量,该变量在Update循环开始时为false,在按下与移动相关的按钮时设置为true。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{

    private float speed = 5.0f;
    private bool isMoving;

    private Animator animator;

    // Start is called before the first frame update
    void Start()
    {
        animator = GetComponent<Animator>();
        speed = 5.0f;
    }

    // Update is called once per frame
    void Update()
    {
        isMoving = false;
        if (Input.GetKey(KeyCode.W))
        {
            transform.Translate(Vector3.forward * Time.deltaTime * speed);
            isMoving = true;
        }
        if (Input.GetKey(KeyCode.S))
        { 
            transform.Translate(-1 * Vector3.forward * Time.deltaTime * speed);
            isMoving = true;
        }

        animator.SetBool("IsMoving", isMoving);

        if (Input.GetKey(KeyCode.A)) { 
            transform.Rotate(0, -1, 0);
        }
        if (Input.GetKey(KeyCode.D))
        {
            transform.Rotate(0, 1, 0);
        }  
    }
}

相关问题