Reputation: 19
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.
The error messages:
Assets\MouseLook.cs(1,26): error CS0246: The type or namespace name 'MonoBehaviour' could not be found
Assets\MouseLook.cs(6,12): error CS0246: The type or namespace name 'Transform' could not be found
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);
}
}
Upvotes: 1
Views: 140
Reputation: 572
You should include the dependencies for the MonoBehaviour and Transform class. Visual Studio or whatever IDE you use cannot recognize the class without the "using" statement in the beginning of your script.
Simply add this to the beginning:
using UnityEngine;
Read more here:
https://learn.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:
https://docs.unity3d.com/ScriptReference/Transform.html
https://docs.unity3d.com/ScriptReference/MonoBehaviour.html
Upvotes: 2