Search Unity

Unit rotation with AddTorque

Discussion in 'Scripting' started by Mernion, Jul 30, 2014.

  1. Mernion

    Mernion

    Joined:
    Jul 6, 2014
    Posts:
    13
    Im trying to make algorithm where unit will be forced to rotate with help of AddTorque function.

    And after a few iterations...Im stuck.

    So, first of all Im calculating a angel between unit position and target.
    After this Im memorising half of this angel to start braking after half of the rotate.

    But, AddTorque works very strange - my unit starts spinning in other direction and rotates not only in one axis.
    What wrong with this code?? Also I would like to make a small roll when unit is rotating (like a plane)

    Code (CSharp):
    1. if (canRotate)
    2. {
    3.    angle = Vector3.Angle(targetDestination, Vector3.forward);
    4.    if (rotateBrakeAngel == 0)
    5.       rotateBrakeAngel = angle/2;
    6.    if (this.rigidbody.angularVelocity.magnitude < maxRotateVelocity)
    7.       {
    8.          Quaternion rotate = Quaternion.LookRotation(targetDestination);
    9.          this.rigidbody.AddTorque(rotate.eulerAngles.normalized * currentEngineForce * Time.fixedDeltaTime);
    10.     }
    11.     else if (angle < rotateBrakeAngel)
    12.     {
    13.            Quaternion rotate = Quaternion.LookRotation(targetDestination);
    14.            this.rigidbody.AddTorque( -rotate.eulerAngles.normalized * currentEngineForce * Time.fixedDeltaTime);
    15.     }
    16. }