- Home /
Resetting rotation with Quaternion.Euler
I'm making a code for my player to be able to look up and down while holding down the up and down arrow keys. I want the rotation to reset and have them return to looking straight ahead when they release the key, but I only want it to reset their vertical rotation so that they continue facing the same way they were before. I thought the code I have would do this, but for some reason it resets the player's rotation on all axes. So if theyre facing south and look up, then they release the arrow key, itll rotate them back to be facing north while looking straight ahead. it's extremely disorienting and I need to fix it, i just don't know how. this is my code (there are other parts to this code, i tried to cut out all irrelevant parts of it):
public float rotateSpeedTwo = 10;
private bool rotatingVertically;
public float MaxAngleUp = 30f;
public float MaxAngleDown = 30f;
CharacterController cc;
void Start ()
{
cc = GetComponent<CharacterController> ();
}
void Update()
{
//look up and down
if (Input.GetKey(KeyCode.DownArrow)) //executes while user holds down the UpArrow
{
//set a lower limit for rotation
if (transform.eulerAngles.x < MaxAngleDown)
{
transform.Rotate(rotateSpeedTwo * Time.deltaTime, 0, 0);
rotatingVertically = true;
}
}
else if (Input.GetKey(KeyCode.UpArrow)) //executes while user holds down the DownArrow
{
//set an upper limit for rotation
if ((transform.eulerAngles.x > (360 - MaxAngleUp)) || transform.eulerAngles.x < 20)
//if ((-transform.eulerAngles.x < MaxAngleUp)) // this won't work as an upper limit
{
transform.Rotate(-rotateSpeedTwo * Time.deltaTime, 0, 0);
rotatingVertically = true;
}
}
if (rotatingVertically)
{
if (Input.GetKeyUp(KeyCode.UpArrow)) //executes the first frame that the user releases the UpArrow
{
transform.rotation = Quaternion.Euler (0,cc.transform.rotation.y,cc.transform.rotation.z); //reset rotation
rotatingVertically = false;
}
else if (Input.GetKeyUp(KeyCode.DownArrow)) //executes the first frame that the user releases the DownArrow
{
transform.rotation = Quaternion.Euler (0,cc.transform.rotation.y,cc.transform.rotation.z); //reset rotation
rotatingVertically = false;
}
}
}
its probably important to note that this script is attached to the player capsule, which uses a character controller. the main camera is a child of the player capsule.
Answer by ian_sorbello · Jun 11, 2015 at 10:50 PM
When you are resetting your rotation, you're not accessing the eulerAngles values, but the Y and Z of the quaternion (which isn't the same). You want:
cc.transform.rotation.eulerAngles.y
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Character movement always faces forwards 1 Answer
My LookRotation forces my character to snap back, and face starting direction after I stop moving? 2 Answers
Particle System Instantiate's With Original Rotation - C# 1 Answer
Make 3D Player look exactly at mouse with ScreenToWorldPoint? (maths Question) 2 Answers