3D top down player rotation error

Hello everyone.
Just for learning, i wanted to do a top down shooter of some sort.
I wanted to do as much as possible by my self, but ofcourse i need to follow some tutorials, that is why i dont know what i have done this time :wink:

Right now i was working on making rotation for my player, so he would always look towards my mouse (so i can also make him shoot that direction and stuff later)
I found some code in a video that i wanted to try, but i end up with an error message.
Here is the Code:

Ray cameraRay = mainCamera.ScreenPointToRay(Input.mousePosition);
        Plane groundplane = new Plane(Vector3.up, Vector3.zero);
        float rayLength;

        if (groundplane.Raycast(cameraRay, out rayLength))
        {
            Vector3 pointToLook = cameraRay.GetPoint(rayLength);
            Debug.DrawLine(cameraRay.origin, pointToLook, Color.blue);
        }

the error message i get is:

The name 'mainCamera' does not exist in the current context

I have not been able to find a way to call mainCamera.
I do have mainCamera in my game folder, but it cannot see it, and i dont know how to call it.

If anyone have an idea on how i can fix this, that would be awesome, thank you :slight_smile:

Here is my whole player script, if you have any other improvements, or maybe another way to do this, than what i have found:

public class PlayerMovement : MonoBehaviour {

    public float moveSpeed;
    public float sprintSpeed;

	void Start () {
        moveSpeed = 3f;
        sprintSpeed = 4.5f;
    }
	
	// Update is called once per frame
	void Update () {
        transform.Translate(moveSpeed*Input.GetAxis("Horizontal")*Time.deltaTime,0f, moveSpeed*Input.GetAxis("Vertical")*Time.deltaTime);

        if(Input.GetKey("left shift"))
        {
            transform.Translate(sprintSpeed * Input.GetAxis("Horizontal") * Time.deltaTime, 0f, sprintSpeed * Input.GetAxis("Vertical") * Time.deltaTime);
        }

        Ray cameraRay = mainCamera.ScreenPointToRay(Input.mousePosition);
        Plane groundplane = new Plane(Vector3.up, Vector3.zero);
        float rayLength;

        if (groundplane.Raycast(cameraRay, out rayLength))
        {
            Vector3 pointToLook = cameraRay.GetPoint(rayLength);
            Debug.DrawLine(cameraRay.origin, pointToLook, Color.blue);
        }

    }
}

Thank you for your time :slight_smile:

The variable mainCamera is nonexistent.

One way to fix this would be to use Camera.main instead of mainCamera. This locates the first active camera in the scene with the tag “MainCamera” (the default scene camera).

The other way would be to write this line of code below [ public float sprintSpeed; ]

public Camera mainCamera;

And then in the inspector set the reference to any camera in the scene.

but the code still does not work for me, i want the charectar to always face the mouse, even tho the WASD shall still be the same direction on screen, and not change with him.
For now my charectar does rotate with my mouse, but now when i fixed that (thanks to SublimeGamer) the wasd also changes with him.
@SublimeGamer