[英]I'm making an FPS game in unity and keep getting these errors
每当我使用此代码时,我都会遇到 2 个错误,我正在调查它们,但我真的找不到任何帮助,将不胜感激。
错误消息:
Assets\MouseLook.cs(1,26):错误 CS0246:找不到类型或命名空间名称“MonoBehaviour”
Assets\MouseLook.cs(6,12):错误 CS0246:找不到类型或命名空间名称“Transform”
编码:
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);
}
}
您应该包括 MonoBehaviour 和 Transform class 的依赖项。 如果没有脚本开头的“使用”语句,Visual Studio 或您使用的任何 IDE 都无法识别 class。
只需将其添加到开头:
using UnityEngine;
在这里阅读更多:
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-directive
您始终可以通过阅读文档了解 class 依赖于什么:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.