- Home /
How can i check/wait until the gameobject will end the rotation ?
IEnumerator Fly()
{
Vector3 p0 = baseTarget.position;
while (true)
{
StartCoroutine(FlyTo(p0));
while (flying)
{
yield return 0;
Vector3 Direction = transform.position - baseTarget.position;
float Height = Direction.y;
if (Height <= 150.0f)
{
var str = Mathf.Min(1.5f * Time.deltaTime, 1);
transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.identity, str);
}
}
yield return new WaitForSeconds(5);
}
}
In this part:
if (Height <= 150.0f)
{
var str = Mathf.Min(1.5f * Time.deltaTime, 1);
transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.identity, str);
}
I want to know when the transform.rotation ended. Then i want to set the variable i have: flying to false that will stop the transform from moving:
flying = false;
This is the Satrt() and the Update() code:
void Start()
{
_rigidbody = GetComponent<Rigidbody>();
baseTarget = GameObject.Find("Base").transform;
lastPos = transform.position;
StartCoroutine(Fly()); // demo routine
}
// calculate bank/turn rotation at Update
void Update()
{
// calculate the displacement since last frame:
Vector3 dir = transform.position - lastPos;
lastPos = transform.position; // update lastPos
float dist = dir.magnitude;
if (dist > 0.001f)
{ // if moved at least 0.001...
dir /= dist; // normalize dir...
float vel = dist / Time.deltaTime; // and calculate current velocity
// bank in the direction of movement according to velocity
Quaternion bankRot = Quaternion.LookRotation(dir + factor * Vector3.down * vel / maxVel);
//transform.rotation = Quaternion.Slerp(transform.rotation, bankRot, turnSpeed * Time.deltaTime);
transform.rotation = Quaternion.Lerp(transform.rotation, bankRot, turnSpeed * Time.deltaTime);
}
}
bool flying = false; // shows when FlyTo is running
// coroutine that moves to the specified point:
IEnumerator FlyTo(Vector3 targetPos)
{
flying = true; // flying is true while moving to the target
Vector3 startPos = transform.position;
Vector3 dir = targetPos - startPos;
float distTotal = dir.magnitude;
dir /= distTotal; // normalize vector dir
// calculate accDist even for short distances
float accDist = Mathf.Min(accDistance, distTotal / 2);
do
{
float dist1 = Vector3.Distance(transform.position, startPos);
float dist2 = distTotal - dist1;
float speed = maxVel; // assume cruise speed
if (dist1 < accDist)
{ // but if in acceleration range...
// accelerate from startVel to maxVel
speed = Mathf.Lerp(startVel, maxVel, dist1 / accDist);
}
else
if (dist2 < accDist)
{ // or in deceleration range...
// fall from maxVel to stopVel
speed = Mathf.Lerp(stopVel, maxVel, dist2 / accDist);
}
// move according to current speed:
transform.position = Vector3.MoveTowards(transform.position, targetPos, speed * Time.deltaTime);
yield return 0; // let Unity breathe till next frame
} while (transform.position != targetPos); // finish when target reached
flying = false; // shows that flight has finished
}
So what i want to do is once the rotation ended/finished stop the transform.
Answer by FlaSh-G · Jun 26, 2017 at 07:11 PM
The way you implemented this, the rotation will never end. In lerp(a,b,t), you have a fixed value for t, while a goes closer to b with every step. But as long as t is not 1 and a does not equal b already, a will never reach b.
If it helps, imagine a = lerp(a, b, 0.5)
. You go 50% of the way with every step, and the remaining way becomes smaller every time. But you will always leave 50% of the way to go, thus you never reach b.
If the constantly slowing down rotation behaviour is what you want, you will have to define a threshold above 0. Once the difference between a and b is below that threshold, you are finished. Use Quaternion.Angle to check that.
You can then skip the remaining 0.00-whatever° degrees by setting a = b;
.