统一动画制作器中的anim.SetBool(“ IsWalking”,walk)错误

时间:2019-03-22 14:07:07

标签: c# unity3d

在Unity学习部分-生存射击游戏中,我得到一个错误,显示以下错误

  

类型“ UnityEngine.Animation”不包含“ SetBool”的定义,找不到类型为“ UnityEngine.Animation”的扩展方法“ SetBool”。您是否缺少装配参考?

我已经检查了文件和组件的名称。

代码

using UnityEngine;

public class PlayerMovement: MonoBehaviour
{
    public float speed = 6f;

    Vector3 movement;
    Animation anim;
    Rigidbody playerRigidbody;
    int floorMask;
    float camRayLength = 100f;

    void Awake()
    {
        floorMask = LayerMask.GetMask("Floor");
        anim = GetComponent<Animation>();
        playerRigidbody = GetComponent<Rigidbody>();
    }

    void FixedUpdate()
    {
        float h = Input.GetAxisRaw("Horizontal");
        float v = Input.GetAxisRaw("Verticle");

        Move(h, v);
        Turning();
        Animating(h, v);
    }

    void Move(float h, float v)
    {
        movement.Set(h, 0f, v);
        movement = movement.normalized * speed * Time.deltaTime;
        playerRigidbody.MovePosition(transform.position + movement);
    }

    void Turning()
    {
        Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit floorHit;

        if(Physics.Raycast (camRay ,out floorHit, camRayLength, floorMask))
        {
            Vector3 playerToMouse = floorHit.point - transform.position;
            playerToMouse.y = 0f;

            Quaternion newRotation = Quaternion.LookRotation(playerToMouse);
            playerRigidbody.MoveRotation (newRotation);

        }
    }

    void Animating(float h, float v)
    {
        bool walking = h!= 0f || v!= 0f;
        anim.SetBool("IsWalking", walking);
    }
}

1 个答案:

答案 0 :(得分:5)

角色的动画控制器的类型为Animator,而不是Animation。在SetBool上使用Animator

Animator anim;

void Awake()
{
    // ...
    anim = GetComponent<Animator>();
    // ...
}