0

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

4
  • localRotation is a Quaternion, but you are passing a float. Commented May 1, 2017 at 8:30
  • Ye, this seems clear, but when I use Quaternion.AngleAxis(); I don't know what to pass in as a second parameter Commented May 1, 2017 at 8:31
  • answers.unity3d.com/questions/1077171/… Commented May 1, 2017 at 8:35
  • Rotate the camera with localEulerAngles. See this answer for a complete code. Just don't like re-posting code unless when really necessary. Commented May 1, 2017 at 8:41

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.