- Home /
FPS Camera Control rotation limits
I am kind of new to Unity and don't have a lot of code experience. I've been working on an FPS game and have written the following code:
using UnityEngine;
using System.Collections;
public class fps_control : MonoBehaviour {
public Rigidbody otherRb;
public float speed;
public float turn;
public float limit_up;
public float limit_down;
public GameObject parentObj;
private float rot;
void Awake () {
Cursor.lockState = CursorLockMode.Locked;
otherRb = parentObj.GetComponent <Rigidbody> ();
float xstart = Input.mousePosition.x;
float ystart = Input.mousePosition.y;
}
void FixedUpdate () {
float xturn = Input.GetAxis("Mouse X");
float yturn = Input.GetAxis ("Mouse Y");
parentObj.transform.Rotate (0f, xturn * turn, 0f);
if (transform.rotation.x <= limit_up) {
if (transform.rotation.x >=limit_down) {
transform.Rotate (-yturn * turn, 0f, 0f);
}
else {
rot = limit_down;
transform.rotation = Quaternion.Euler (rot,0f,0f);
}
}
else {
rot = limit_up;
transform.rotation = Quaternion.Euler (rot,0f,0f);
}
}
}
The problem is that the rotation limits I tried to set for the camera aren't working (resulting in the character doing some crazy somersaults). What am I doing wrong? Please help, and thank you to anyone who answers (even if your answer is crap... I still appreciate your effort).
I've approved in the hopes that'll you'd edit your post to include relevant code and clean up the formatting.
The Standard Assets package has a $$anonymous$$ouseLook script that uses rotation limits.
I looked into it, but I think it would be better if I just tried to find the problem with this script. I'm sure that the Standard Assets $$anonymous$$ouseLook script is great, but if I don't find out what's wrong with this script, then I'll likely encounter other problems in the future that can't be solved just by downloading something from the Assets Store.
If I have to, though, I may try that script. Thank you for responding.
The first problem I see is that you are using transform.rotation.x which is a Quaternion (w, x, y, z) while you probably want to use transform.eulerAngles.x.
However you have a deeper problem when you assume that setting the rotation of a transform to (x, y, z) will return a rotation of (x, y, z). Unfortunately multiple (x, y, z) combinations can actually represent the same rotation which can lead to weird results, for instance (90, 0, 0) and (90, 180, 180) are the same rotation. If you want to store a euler rotation then use a Vector3 in your class ins$$anonymous$$d of relying on the Quaternion transform.rotation to preserve all the data. Then you can assign transform.eulerAngles = storedEulerAngles while replacing things like
Vector3 eulerRotation = transform.eulerAngles;
and
if(transform.eulerAngles.x<0)
with
Vector3 eulerRotation = storedEulerAngles ;
and
if(storedEulerAngles .x<0)
Well, I got it working. Thanks for your answer! However, there are 2 new (but fortunately smaller) problems. The first one is that I have to choose really weird numbers for the upper and lower limits or else the camera gets stuck. The other one is that the camera seems to vibrate when it's at the upper or lower limits. Here is the updated code:
using UnityEngine;
using System.Collections;
public class fps_control : $$anonymous$$onoBehaviour {
public Rigidbody otherRb;
public float speed;
public float turn;
public float limit_up;
public float limit_down;
public GameObject parentObj;
private Vector3 angles;
void Awake () {
Cursor.lockState = CursorLock$$anonymous$$ode.Locked;
otherRb = parentObj.GetComponent <Rigidbody> ();
float xstart = Input.mousePosition.x;
float ystart = Input.mousePosition.y;
}
void FixedUpdate () {
float xturn = Input.GetAxis("$$anonymous$$ouse X");
float yturn = Input.GetAxis ("$$anonymous$$ouse Y");
parentObj.transform.Rotate (0f, xturn * turn, 0f);
if (angles.x <= limit_up) {
transform.Rotate (-yturn * turn, 0f, 0f);
}
else if (angles.x >=limit_down) {
transform.Rotate (-yturn * turn, 0f, 0f);
}
else if (angles.x < 180) {
angles.x = limit_up;
transform.localEulerAngles= angles;
}
else {
angles.x = limit_down;
transform.localEulerAngles = angles;
}
angles = transform.localEulerAngles;
}
}
What exactly am I doing wrong? It's better than before, but it does need to be fixed.
Answer by Caleb_1701 · Mar 30, 2016 at 12:02 AM
For anyone wanting to see the answer to this question, look in the comments. I'm only posting this to close the question, because maccabbe's answer was posted in the comments instead of here.
Your answer
Follow this Question
Related Questions
How do I properly rotate an FPS camera with a joystick? 1 Answer
Forward and back movements with a camera emulating an isometric view 1 Answer
Mouse axes based on position, not movement. 1 Answer
How Do i Prevent The First Person Camera From Turning 0 Answers
Camera rotates when I run into a wall 0 Answers