- Home /
Problems after rotating character.
I honestly can't find what's wrong with it, but sometimes after rotating, the character randomly stops moving.
Here's my code (specifically the function where the character rotates):
public IEnumerator TurnToTarget(Vector3 target, float rotDuration)
{
// wait until the hunter can move
while(!IsMoving)
{
yield return null;
}
// set states
States previousState = state;
state = States.turning;
// the hunter is turning
IsTurning = true;
print ("turning");
// the starting rotation
Quaternion startRotation = transform.rotation;
// the rotation needed to face the target
Quaternion newRotation = Quaternion.LookRotation(target - transform.position, Vector3.up);
print ("set lookrotation");
// test if the rotation is equal
if(startRotation == newRotation)
{
print ("startRotation == newRotation, cancel turn");
// then the hunter has no reason to turn, so he doesn't
IsTurning = false;
state = previousState;
// exit the coroutine
yield break;
}
print ("elapsed time = 0");
// set the elapsed time var to 0
float elapsedTime = 0.0f;
// turning loop
while (elapsedTime < rotDuration)
{
while(!IsMoving)
yield return null;
print ("elapsed time: "+elapsedTime+"\nrotation: "+transform.rotation);
// add time elapsed since last frame to elapsed time var
elapsedTime += Time.deltaTime;
float t = elapsedTime / rotDuration;
// Slerp to the target
print ("slerping");
transform.rotation = Quaternion.Slerp(startRotation,newRotation,t);
// return here next frame
yield return null;
}
print ("done turning");
// when done, turning is false
IsTurning = false;
state = previousState;
}
If someone could tell me what I'm doing wrong here, I'd really appreciate it.
Have you checked Is$$anonymous$$oving? If it does not get set back to false, this method will hang.
I think you may be right about that, but the only reason for it not being set I can think of would be another coroutine not finishing.
Since Is$$anonymous$$oving is not set in this code, there is no way for us to debug the issue.
That's the other coroutine. I'd rather not post the entire script, since it is over 700 lines.
public IEnumerator Pause$$anonymous$$ovement(float time)
{
// the hunter should not move
Is$$anonymous$$oving = false;
Camera.main.GetComponent().hunter$$anonymous$$ove = false;
// wait a while
yield return new WaitForSeconds(time);
// we don't want him to still follow the critter
if(!CenterCollision)
CenterTarget();
// now the hunter can move again
Is$$anonymous$$oving = true;
Camera.main.GetComponent().hunter$$anonymous$$ove = true;
}
I don't see anything here to cause the problem. $$anonymous$$ake Is$$anonymous$$oving public (if it isn't currently) and watch it in the inspector to test if this is the problem.
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
Flip over an object (smooth transition) 3 Answers
Rotating Character 1 Answer
Orbit Camera Around Player And Add Force 3 Answers
Rotate towards movement direction 1 Answer