Question3r
Question3r

Reputation: 3772

Simple FPS camera movement in Unity

I started a new project. It's a simple FPS game. Tutorials are often outdated so i started to write it on my own.

The player's movement is fine. The camera's is not. I store all my data values in a data class

public class CameraMovementData : CameraCommonData // datastore
{
    public float InputHorizontal { get { return Input.GetAxisRaw("Mouse X"); } } // horizontal mouse input

    private float yMin = -70; // player looks down - minimum
    private float yMax = 70; // player looks up - maximum
    public float InputVertical { get { return Mathf.Clamp(Input.GetAxisRaw("Mouse Y"), yMin, yMax); } } // vertical clamped mouse Input

    public float Sensitivity { get { return 30; } } // mouse sensitivity

    public Vector2 Movement { get { return new Vector3(InputHorizontal * Sensitivity * Time.deltaTime, -InputVertical * Sensitivity * Time.deltaTime); } } // the movement direction of the camera
}

In my controller I have this:

public class CameraMovementController : CameraCommonController // use the values from the datastore
{
    private CameraMovementData data; // instance to the datastore

    private void Start()
    {
        data = new CameraMovementData();
    }

    private void Update()
    {
        data.PlayerTransform.localRotation = data.Movement.x; // Rotate the player on X
        data.CameraTransform.localRotation = data.Movement.y; // Rotate the camera on Y
    }
}

So as you can see, I don't know, how to write my rotation in the Update method.

I have to use the localRotation, right?

And what do I have to assign to it, to make it work?

It seems I have to use

Quaternion.AngleAxis();

but I don't know, what to pass in as a second parameter

Upvotes: 0

Views: 1406

Answers (1)

lighthousand
lighthousand

Reputation: 26

You will often need to attach the Camera to the Player object so you do not have to explicitly retrieve data about player's rotation. That being said if the Camera is the child of the Player object, then when the Player is rotating the Camera will also rotate. What you will have to do after is just to clamp the rotation on the X Axis so it won't roll over. I hope this will clear some problems you might have.

Upvotes: 1

Related Questions