如何使用 Unity 玩家移动脚本解决此问题?

问题描述 投票:0回答:1

我正在为玩家身体创建运动,完成后我单击保存并返回 Unity,但有红色文本说:名称“z”在当前上下文中不存在。 “z”表示 Z 轴,我不知道该怎么做,我几乎尝试了我想到的一切,请我真的需要帮助。差点忘了我正在制作 Unity 3D 游戏。

这是运动的脚本:

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

public class PlayerMovement : MonoBehaviour
{
    public CharacterController controller;

    public float speed = 5f;
    public float gravity = -9.81f;

    public Transform groundCheck;
    public float groundDistance = 0.4f;
    public LayerMask groundMask;

    Vector3 velocity;
    bool isGrounded;

    // Update is called once per frame
    void Update()
    {
        isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

        if(isGrounded && velocity.y < 0);
        {
            velocity.y = -2f;
        }

        float x = Input.GetAxis("Horizontal");
        float y = Input.GetAxis("Vertical");

        Vector3 move = transform.right * x + transform.forward * z;

        controller.Move(move * speed * Time.deltaTime);

        velocity.y += gravity * Time.deltaTime;

        controller.Move(velocity * Time.deltaTime);
    }
}

我不知道我想做什么,因为我忘记了,我第二天会写这篇文章。

c# unity-game-engine scripting
1个回答
0
投票

错误告诉您“z”不存在,因为它不存在。您根本没有在代码中定义名为“z”的变量。

很明显,您打算将“y”变量用作“z”,因为您已经定义了它,但没有在任何地方使用它。

您的 IDE 中启用了 IntelliSense 吗?任何适当的代码编辑器都会告诉您该变量并不像您键入的那样。

确保您启用了这些必要的工具,并相信错误消息! 如果您收到错误消息,告诉您变量不存在,那么您可能应该对此进行调查。

© www.soinside.com 2019 - 2024. All rights reserved.