Rotate the player around the z axis, using a mouse look type script
I've done a lot of research looking for an answer on this already, but am yet to find one. I have a game where you press a button and the player will flip gravity, all good. However when I do this I obviously want the player to rotate along with it so it looks right when walking on the ceiling. I have managed to get it to simply rotate to a fixed 180 degrees in the z axis, but to look better I want to use deltaTime in order to gradually rotate the character. Now I have been able to partially do this but the player object becomes tilted and not at 0 when the x axis changes to -180. With this, the whole rotation values changes as you rotate the camera, where the z axis and x axis keep changing between 180 and -180 degrees when the y axis changes between 90 degrees. Another thing is when rotating via 180 * Time.deltaTime, the rotation doesn't really hit 180. It's either too short or if I have the rotation continuously going it goes from 179 degrees to -179 degrees and doesn't seem to hit 180. The images show when the player is upside down, depending on the y value the values of X and Z are different, the top being the desired rotation continuously and the bottom being what I want to get rid of essentially. All I want is a gradual rotation along Z so the player can be upside down and everything can work the same regardless of whether you're on the roof or on the floor, ideally while still being able to look around like you normally would while you are switching gravity.
The code is as follows:
void Update () {
Vector2 mouseDirection = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse
Y"));
mouseDirection = Vector2.Scale(mouseDirection, new Vector2(sensitivity * smoothing, sensitivity
* smoothing));
smoothV.x = Mathf.Lerp(smoothV.x, mouseDirection.x, 1f / smoothing);
smoothV.y = Mathf.Lerp(smoothV.y, mouseDirection.y, 1f / smoothing);
mouseLook += smoothV;
if (Input.GetKeyDown(KeyCode.G))
{
gravityFlipped = !gravityFlipped;
}
if (gravityFlipped)
{
if (zRotation > 179)
{
zRotation = 180;
}
else
{
zRotation += 180 * Time.deltaTime;
}
mouseLook.y = Mathf.Clamp(mouseLook.y, -70f, 70f);
player.transform.localRotation = Quaternion.AngleAxis(mouseLook.x, Vector3.up) *
Quaternion.AngleAxis(zRotation, Vector3.forward);
transform.localRotation = Quaternion.AngleAxis(-mouseLook.y, Vector3.right);
}
else
{
if (player.transform.localRotation.z == 0)
{
mouseLook.y = Mathf.Clamp(mouseLook.y, -70f, 70f);
transform.localRotation = Quaternion.AngleAxis(-mouseLook.y, Vector3.right);
player.transform.localRotation = Quaternion.AngleAxis(mouseLook.x, player.transform.up);
}
else
{
if (zRotation < 0)
{
zRotation = 0;
}
else
{
zRotation -= 180 * Time.deltaTime;
}
mouseLook.y = Mathf.Clamp(mouseLook.y, -70f, 70f);
player.transform.localRotation = Quaternion.AngleAxis(mouseLook.x, Vector3.up) *
Quaternion.AngleAxis(zRotation, Vector3.forward);
transform.localRotation = Quaternion.AngleAxis(-mouseLook.y, Vector3.right);
}
}
Your answer
Follow this Question
Related Questions
Rotating Two Axes for Gravity,Rotating only two axes towards Object 0 Answers
Rotating on other axis 1 Answer
,rotate object to match another object on its local 3 axis 1 Answer
Rotate an Object while moving forward using empty gameObject 0 Answers
My rotation is not the same in inspector as it is in the scirpt 0 Answers