Convert function into a While loop


I have this function that rotates the top half of the Mech back facing forward to match the bottom half after an attack -

function rotateTurret(){

    var lookPos = transform.forward;
    var rotation = Quaternion.LookRotation(lookPos);

    rotation *= Quaternion.FromToRotation(Vector3.up, Vector3.right);
    rotation *= Quaternion.Euler(XattackDegrees, YattackDegrees, 0);
    mechTurret.rotation = Quaternion.Slerp(mechTurret.rotation, rotation, Time.deltaTime * 2);

What I want to do is covert it into a while function, I thought I could add something like -

while(mechTurret.rotation != transform.forward){

But Unity wont allow that, or -

while(mechTurret.rotation != transform.rotation){

But Unity wont allow that ether, can someone point me in the right direction please, thanks.

Firstly checking for an equality will likely never work (or take far longer than you expect).

Also your syntax for the condition is trying to do a !mechTurret.rotation - which makes no sense :slight_smile:

I think you meant:

     while(mechTurret.rotation != transform.rotation) //Still not a good idea


     while(!(mechTurret.rotation == transform.rotation))

You can try:

     while((mechTurret.eulerAngles - transform.eulerAngles).sqrMagnitude > 0.1f)