How to change rotation speed

I’m trying to make a tank, and it works pretty well, I just wished that the turret would rotate a bit slower. Is it possible to change the rotation speed?
Here’s the code I have:

    void Update()
    {
        Vector3 mousePos = Input.mousePosition;
        mousePos.z = 5.31f;

        Vector3 objectPos = Camera.main.WorldToScreenPoint (transform.position);
        mousePos.x  = mousePos.x - objectPos.x;
        mousePos.y = mousePos.y - objectPos.y;
        
        float angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
        transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
    }
1 Like

Your code must depend on user frame rate to play for each user the same way. You can achieve this by multiplying Time.deltaTime.

On the provided, you can see tips for doing Rotation. Also check this with Rotation example: Unity - Scripting API: Transform.rotation

Lines 10 and 11 above compute the angle and instantly set it.

Instead, use this pattern and (most likely) Mathf.MoveTowardsAngle() to get a gradual change:

Smoothing movement between any two particular values:

You have currentQuantity and desiredQuantity.

  • only set desiredQuantity
  • the code always moves currentQuantity towards desiredQuantity
  • read currentQuantity for the smoothed value

Works for floats, Vectors, Colors, Quaternions, anything continuous or lerp-able.

The code: SmoothMovement.cs · GitHub

EDIT: you also should only do those lines if mousePos is greater than a certain amount after you subtract the objectPos from it, something like:

if (mousePos.magnitude > 0.1f)
{
  /// ... do the smoothing code here, what was lines 10 / 11 above
}

So I’d have to do the thing from that GitHub doc, set the desiredQuantity to the angle of the cursor, and then

currentQuantity = Mathf.MoveTowardsAngle(
			currentQuantity,
			desiredQuantity,
			MovementPerSecond * Time.deltaTime);

Or will that not work?

Yes, that is exactly what you want.

And you would use the currentQuantity (you should probably name it currentAngle for sanity sake) for your Quaternion.Euler() call.

Speaking of sanity and naming, you may want to get in the habit of making extra named variables, as a new relativeDirection Vector3, rather than modifying the mousePos … yes, it’s easy and cheesy to see what you did above, but it would be clearer if there was a whole fresh variable.

Vector3 relativeDirection = mousePos - objectPos;

That way if later in the function you go to use mousePos, its meaning isn’t suddenly different because you subtracted objectPos halfway through things.

A lot of compilers will generate nearly (or exactly) the same code when they see either one.

And while we’re at it, I just noticed that 5.31f magic number… that’s sorta… sub-optimal. Here’s a blurb on magic numbers:

Don’t use Magic Numbers:

Instead, know where the number comes from and compute it from the correct inputs, or at least extract it to somewhere semantically meaningful so you have a fighting chance of understanding it in the future should it need to change.

Does the float angle need to be converted to something else because I made desiredAngle equal the float angle, then put that through the ProcessMovement() with Mathf.MoveTowardsAngle and then put ProcessMovement() back in Update(), but it doesn’t seem to do anything.

About the magic number; I’m quite new to this so I didn’t write too much of the code myself.
However I was curious earlier so I checked it out but it doesn’t seem to do anything, I set it to 0 and nothing happened, and I set it to 100 and nothing happened.

That’s just a bug for you to debug and figure out why the data isn’t changing. You have reference code from the github to test against. See below for how to debug.

Sample code is great, but there are two steps to it, not just one:

Two steps to tutorials and / or example code:

  1. do them perfectly, to the letter (zero typos, including punctuation and capitalization)
  2. stop and understand each step to understand what is going on.

If you go past anything that you don’t understand, then you’re just mimicking what you saw without actually learning, essentially wasting your own time. It’s only two steps. Don’t skip either step.


Sounds like you wrote a bug… and that means… time to start debugging!

By debugging you can find out exactly what your program is doing so you can fix it.

Use the above techniques to get the information you need in order to reason about what the problem is.

You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.