[英]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.