- Home /
The question is answered, right answer was accepted
Doing a proper 180 smooth turn
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.
Thanks a lot for helping me with the flickering, i dont know what i expected putting it into Update (). $$anonymous$$ust've been late at night. Anyway, the other problem was occurring because i was incorrectly freezing the camera move script (the turn values werent restored properly on new rotation) so the X axis was flickering. You can add your answer about startcoroutine in update () so i can accept it.
A small issue, but use yield return null
rather than yield return 0
, since the former has no allocations.
Answer by robertbu · Sep 28, 2013 at 04:00 AM
Your flicker issue is to to line 30 and 31. A StartCoroutine() is called just once. You are calling it every update during the time the rotation is running. Depending on your frame rate, during the .2 seconds you are stacking up between 5 and 25 coroutines. Fortunately they are all doing the same thing.
As for your rotation issue, I don't understand it from your description. I'd have to see the camera code, and perhaps a video or play with a sample project. If I can understand the issue, a fix should be easy.
Follow this Question
Related Questions
How to make camera position relative to a specific target. 1 Answer
Help with camera coding - Modern Zelda-style camera 1 Answer
GUI controlling other game objects 0 Answers
Help Ship Controll 0 Answers
Player controller 0 Answers