Search Unity

Help - Rotate with closest rotation value?

Discussion in 'Scripting' started by nevaran, Dec 19, 2014.

  1. nevaran

    nevaran

    Joined:
    May 21, 2010
    Posts:
    247
    Hey guys, ive been trying to get a torque rotation to work, but I stumbled into a nasty problem:
    The rotation is decent, everything seems to work, but when the object rotates 180 or -180 degreese ,and continues, it does a quick turn the opposite way, to get to the new value(which is reverse to the last one) - it works as a rotation clamp of a sort, mouse look even uses it and its fine, but here it has a huge problem.

    If anyone can help solving this problem, would be awesome :)

    Here is the code where I do the rotation calculations:

    normsX += Input.GetAxis("Mouse X");
    if(normsX < -360){
    normsX = 360;
    }
    if(normsX > 360){
    normsX = -360;
    }
    Quaternion targetRot = turret.Turret.transform.rotation;
    targetRot.y = normsX / 360;
    float rotationDir = (targetRot.y - turret.Turret.transform.rotation.y);
    turret.Turret.AddTorque(Vector3.up * rotationDir * turret.turretTurnSpeed * turret.Turret.angularDrag * Time.fixedDeltaTime, ForceMode.VelocityChange);
     
    Last edited: Dec 19, 2014