Having trouble rotating camera around player in 180° increments
I have a sidescrolling 2D game in which you can rotate the world by 90° or 180°. The result is that I can connect a bunch of corridors along the X and Z grid, and rotate 90° at their intersections to allow you to switch between the corridor along the X axis, and the one along the Z axis. I want each rotation to be lerped instead of instant.
90° rotation works like a charm. If I press the positive rotation button the world rotates +90°, and -90° with the negative rotation button. I can keep pressing a direction and the world always rotates in that direction.
But 180° rotation doesn't work, regardless of which button (positive or negative) I press, it rotates once +180° and the next -180°, then +, then -, then +, and so on. I would like for this rotation to also work like the 90° rotation so that it always rotates in the direction I'm pressing rather than alternating.
I suspect it has something to do with either how I wrote the transform, or based on things I've read in the forum about the engine selecting the shortest path from the current rotation to the target rotation, but this is my first venture into game development so it's likely that my transform is the culprit, haha. Here's the troubling code:
// I have a player GameObject that has a camera as a child. I move and rotate the player
// only, without touching the camera itself. This is the hierarchy
//
// Player
// -- Camera
//
// This script is on the player. This excerpt is the rotation code only,
// I think movement is irrelevant and works fine.
void Update () {
if (Input.GetButtonDown ("Camera Rotation")) {
float rotationDirection = Input.GetAxisRaw ("Camera Rotation");
if (rotationDirection < 0) {
targetAngle += 180f; // or 90f depending on some condition being met
} else {
targetAngle -= 180f; // or 90f, again, based on same condition
}
// Basically what happens above is I check which direction I pressed,
// and whether it should rotate by 180° or 90°. The code below is what
// I think I'm doing wrong
}
// Again, this.transform is the player's transform
this.transform.eulerAngles = new Vector3( 0,
Mathf.LerpAngle(this.transform.eulerAngles.y,
targetAngle,
rotationSpeed * Time.deltaTime),
0);
}
Any help is appreciated!
TLDR: Right bumper rotates world by n°, Left bumper rotates world by -n°, and should do so for as many times as I press the bumper. Rotation by 90° works great, but rotation by 180° rotates once left, then right, and so on every time I press a bumper.
Thank you in advance for anyone who is willing to help me out! :)
Your answer
Follow this Question
Related Questions
Rotating Skybox Issues? 1 Answer
How to set clockwise or counterclockwise rotation? 0 Answers
2d keep player upright 1 Answer
Object rotate and slowly stop to toward 1 Answer
Applying AddForce() while rotating player gameobject. 0 Answers