I have this code to rotate an object towards another at a given speed.

    /// <summary>
    /// Rotate towards the target.
    /// </summary>
    /// <param name="lookPosition"></param>
    private void RotateTowardsTarget(Vector3 lookPosition)
        // Determine which direction to rotate towards
        Vector3 targetDirection = lookPosition - transform.position;

        // The step size is equal to speed times frame time.
        float singleStep = weaponRotationSpeed * Time.deltaTime;

        // Rotate the forward vector towards the target direction by one step
        Vector3 newDirection =
            Vector3.RotateTowards(turretYRotation.transform.forward, targetDirection, singleStep, 0.0f);

        // Draw a ray pointing at our target in
        //Debug.DrawRay(turretYRotation.transform.position, newDirection, Color.red, 3);

        newDirection.y = 0;
        // Calculate a rotation a step closer to the target and applies rotation to this object
        turretYRotation.transform.rotation = Quaternion.LookRotation(newDirection);

And I'd like to convert this to a "Degrees per second" to give the user useful feedback on how quickly it rotates. Is there a formulae I can apply to work it out?

  • \$\begingroup\$ Do you want weaponRotationSpeed to be in degrees per second, or do you want to return how fast it is rotating? \$\endgroup\$ – bornander Jun 8 '20 at 20:41

Currently weaponRotationSpeed defines the rotation speed in radians per second, if what you're asking is to let this variable define rotation speed in degrees per second you should change

float singleStep = weaponRotationSpeed * Time.deltaTime;


float singleStep = (weaponRotationSpeed * (180/Math.PI)) * Time.deltaTime;
  • \$\begingroup\$ You can also use Mathf.Rad2Deg \$\endgroup\$ – DMGregory Jun 4 '20 at 11:18
  • \$\begingroup\$ Not quite, what I'd like tot do is leave the rotation code as it is, but have a way to work of "Given weaponRotationSpeed is X, that means the turret is rotating at Y degress a second", so I can display that to the user \$\endgroup\$ – TMH Jun 4 '20 at 16:59
  • \$\begingroup\$ So a simple radians to degrees conversion, i.e. Y = X * (180 / Math.PI), or as DMGregory stated Y = Mathf.Rad2Deg(X). \$\endgroup\$ – Robin Seibold Jun 4 '20 at 20:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.