Hello. I’m writing a first person game in which i want to implement an ability to do a 180 degree turn when a button is pressed.
I have this script to do it so far:
public Quaternion playerStartRot;
public Quaternion playerEndRot;
public float time;
public bool working = false;
private GameObject player;
IEnumerator SmoothRotate ()
{
float i = 0;
float rate = 1/time;
while (i < 1)
{
i += Time.deltaTime * rate;
player.transform.rotation = Quaternion.Slerp (playerStartRot, playerEndRot, i);
yield return 0;
}
working = false;
}
void Start ()
{
player = transform.parent.gameObject;
}
void Update ()
{
if (working)
StartCoroutine(SmoothRotate());
}
And here’s the part of script that invokes this one:
if (Input.GetButton("TurnAround"))
{
var camOverride = cam.GetComponent<Player_CameraOverride>();
if (camOverride != null && !camOverride.working)
{
camOverride.playerStartRot = transform.rotation;
camOverride.playerEndRot = transform.rotation * Quaternion.AngleAxis(180, transform.up);
camOverride.time = .2f;
camOverride.working = true;
}
}
Camera is a child of the Player object. In my camera control script i turn my player object around horizontally and camera vertically, as a result making the whole object turn horizontally (since camera is a child) and camera also turning vertically in its own space. I also freeze that script for the 180 turn so it wont interfere.
At the moment, this script turns my player model 180 degrees horizontally but also resets camera’s vertical rotation. I now want to keep the camera rotation the way it was, because currently it resets to 0,0,0 when the script starts. I’ve tried many ways to do it, but, apparently im horribly awful with Quaternions and just cant to understand them.
Whats the right way to do it?
Also, my camera seems to flicker when i press the button a lot of times, despite the “working” bool check in the invoke.