简体   繁体   English

僵尸玩具运动脚本不起作用

[英]Zombie Toys movement script not working

I linked pictures of my error codes I've tried everything. 我链接了我尝试过的所有错误代码的图片。 Please someone look at it and let me know what I did wrong. 请有人看看,让我知道我做错了什么。 I'm trying to do the Zombie Toys game [Error][Error][Error][Error][Error][Error] 我正在尝试做僵尸玩具游戏[错误] [错误] [错误] [错误] [错误] [错误]

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

Variable names are case-sensitive in C#. 变量名称在C#中区分大小写。

You declared the Vector3 variable as playertoMouse but then the next line you are trying to use playerToMouse with playerToMouse.y = 0f; 您将Vector3变量声明为playertoMouse但是接下来尝试在playerToMouse.y = 0f;使用playerToMouse playerToMouse.y = 0f; . Notice that the T in To is capitalized instead of lower-case t as declared. 请注意, To中的T大写,而不是声明的小写t

I suggest you capitalize that t everywhere so that to becomes To as that's more easier to read than making it a lower-case t . 我建议您到处都大写该ttoTo变成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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM