简体   繁体   中英

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.

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);
    }
}

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://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:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM