Zombie Toys运动脚本不起作用

时间:2017-06-22 02:51:48

标签: unity3d

我将错误代码的图片链接到了我尝试过的所有内容。请有人看一下,让我知道我做错了什么。我试图做Zombie Toys游戏[错误] [错误] [错误] [错误] [错误] [错误]

using UnityEngine;

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

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

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

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

        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 :(得分:0)

变量名在C#中区分大小写。

您将Vector3变量声明为playertoMouse,但接下来要将playerToMouseplayerToMouse.y = 0f;一起使用。请注意,T中的To是大写的,而不是声明的小写t

我建议您将t随处变为大写,以便to变为To,因为它比将其设为小写t更容易阅读。

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);
    }
}