简体   繁体   English

我正在统一制作 FPS 游戏并不断收到这些错误

[英]I'm making an FPS game in unity and keep getting these errors

Whenever I use this code I get 2 errors I was looking into them but I couldn't really find much any help would be greatly appreciated.每当我使用此代码时,我都会遇到 2 个错误,我正在调查它们,但我真的找不到任何帮助,将不胜感激。

The error messages:错误消息:

Assets\MouseLook.cs(1,26): error CS0246: The type or namespace name 'MonoBehaviour' could not be found Assets\MouseLook.cs(1,26):错误 CS0246:找不到类型或命名空间名称“MonoBehaviour”

Assets\MouseLook.cs(6,12): error CS0246: The type or namespace name 'Transform' could not be found Assets\MouseLook.cs(6,12):错误 CS0246:找不到类型或命名空间名称“Transform”

The code:编码:

public class MouseLook : MonoBehaviour
{
    public float sensX;
    public float sensY;

    public Transform orientation;

    float xRotation;
    float yRotation;

    private void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;
    }
    private void Update()
    {
        // Get mouse input
        float mouseX = Input.GetAxisRaw("Mouse X") * Time.deltaTime * sensX;
        float mouseY = Input.GetAxisRaw("Mouse Y") * Time.deltaTime * sensY;

        yRotation += mouseX;

        xRotation -= mouseY;
        xRotation -= Mathf.Clamep(xRotation, -90f, 90f);


        // rotate cam and orientation
        transform.rotation = Quaternion.Euler(xRotation, yRotation, 0);
        orientation.rotation = Quaternion.Euler(0, yRotation, 0);
    }
}

You should include the dependencies for the MonoBehaviour and Transform class.您应该包括 MonoBehaviour 和 Transform class 的依赖项。 Visual Studio or whatever IDE you use cannot recognize the class without the "using" statement in the beginning of your script.如果没有脚本开头的“使用”语句,Visual Studio 或您使用的任何 IDE 都无法识别 class。

Simply add this to the beginning:只需将其添加到开头:

using UnityEngine;

Read more here:在这里阅读更多:

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-directive https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-directive

You can always find out what a class depends on by reading the docs:您始终可以通过阅读文档了解 class 依赖于什么:

https://docs.unity3d.com/ScriptReference/Transform.html https://docs.unity3d.com/ScriptReference/Transform.html

https://docs.unity3d.com/ScriptReference/MonoBehaviour.html https://docs.unity3d.com/ScriptReference/MonoBehaviour.html

暂无
暂无

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

相关问题 我正在统一制作 FPS 游戏并不断收到 1 个错误 - I'm making an FPS game in unity and keep getting 1 error 我已经遵循了关于为统一的fps游戏制作控件的教程。 控制工作但如果我离开控件然后我继续向左移动 - I have followed a tutorial on making controls for a fps game on unity. the controls work but if I leave the controls then I keep moving to the left 在 Unity 中制作现代 FPS 游戏健康再生系统 C# - Making a Modern FPS game health regeneration system in Unity C# 我在 Unity 中制作游戏时遇到了 C# 中的 OnCollisionEnter 函数问题 - I'm having a problem with the OnCollisionEnter function in C# while making a game in Unity 我正在用 Unity 程序制作节奏游戏。 这是一个关于长笔记的问题 - I'm making a rhythm game with Unity program. It's a question about long notes 在我统一制作的 2D 平台游戏中,我的玩家被困在墙内 - My player is getting stuck inside the wall in the 2D platforming game I am making in unity 我正在尝试将Java脚本统一转换为csharp脚本,但出现一些错误? - I'm trying to convert java script in unity to csharp script but getting some errors? 我的统一 2d 运动脚本中有很多错误 - I'm getting so many errors in my unity 2d movement script 我正在尝试使用 Unity 制作 FPS 足球游戏,但我的脚本不起作用 - I am trying to make an FPS soccer game using Unity, but my script isn't working 我正在做一个游戏,我可以/应该创建一个“敌人”课程吗? - I'm making a game, can/should I create an “Enemy” class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM