Change Speed for Rotatating Character 90 degrees (Isometric view)

I realize there are a lot of questions about this but I have searched through and used every method previously suggest and it has not worked.

Each method, except for the one I currently use, only rotates the character by 1 degree when using “GetKeyDown”. If I use “GetKey” and hold down the key it rotates the character as long as the key is held but I can change the speed of that.

However neither one accomplishes what I am trying to do:

  • Single keypress does 90 degree rotation (accomplished)

  • Change speed of rotation (can’t figure out)

My current method is the only one I’ve found that seems to work. It will rotate the character a full 90 degrees left or right however I can’t change how fast that occurs. I try adding “Time.deltaTime” but it ends up making it break like the other methods (which also use TdT).

Here is my method:

privatefloat targetAngle =0;
constfloat rotationAmount =1.5f;
publicfloat rSpeed =1.0f;

void Update ()
{
if(Input.GetKeyDown(KeyCode.Q)){targetAngle -=90.0f;}
if(Input.GetKeyDown(KeyCode.E)){targetAngle +=90.0f;}
if(targetAngle !=0){Rotate();}
}

protectedvoidRotate()
{
  if(targetAngle>0)
  {
   transform.RotateAround(transform.position,Vector3.up,-rotationAmount);
   targetAngle -= rotationAmount;
  }
  if(targetAngle <0)
  {
   transform.RotateAround(transform.position,Vector3.up, rotationAmount);
   targetAngle += rotationAmount;
  }
}

I’ve tried adding Time.deltaTime * rSpeed to rotationAmount but that doesn’t do anything. I truly have no idea what to do.

As I said other methods either make it rotate one degree every key press, rotate as the key is held down, or instantly rotate it.

I want one keypress to be a 90 degree rotation but to change how fast it makes that rotation occur.

Can you describe how it “breaks”? If the rotation never happens it means your rotationAmount is too low.

It’s not that the rotation never happens. Is that when I add Time.deltaTime it becomes slower or only rotates 1 degree no matter what I set the for rotationAmount (or speed for that matter).

Did you forget to apply deltaTime before adding/subtracting rotationAmount from targetAngle?

That’s what I tried.

Maybe you can put this on a cube in unity and see your results? Should work easily.

I would personally just do it this way. Change GetKeyDown to GetKey if you want it to keep rotating while the key is pressed.

public const float RotationDegrees = 90.0f;

public float RotationSpeed = 2.5f;
public bool IsRotating = false;
public Quaternion TargetRotation = Quaternion.identity;

void Update()
{
    if (!IsRotating)
    {
        if (Input.GetKeyDown(KeyCode.Q))
        {
            TargetRotation = transform.rotation * Quaternion.Euler(0.0f, RotationDegrees, 0.0f);
            IsRotating = true;
        }
        else if (Input.GetKeyDown(KeyCode.E))
        {
            TargetRotation = transform.rotation * Quaternion.Euler(0.0f, -RotationDegrees, 0.0f);
            IsRotating = true;
        }
    }
}

void FixedUpdate()
{
    if (IsRotating)
    {
        if (Quaternion.Angle(transform.rotation, TargetRotation) > 0.0f)
        {
            transform.rotation = Quaternion.RotateTowards(transform.rotation, TargetRotation, RotationSpeed);
        }
        else
        {
            IsRotating = false;
        }
    }
}
1 Like

Does the precision loss add up over the gameplay time? As in if I press it a 100 times in the same playthrough it will be significantly less precise?

Add rounding on the angles? I’m not sure what you mean by that to be honest. Thank you though!

It seems like Quaternion.RotateTowards doesn’t rotate correctly when used in Update due to the maxDegreesDelta. If you attach the old script I posted to a cube and rotate it a few times it occasionally doesn’t complete the rotation.

P.S. I updated my previous reply with a fix that seems to be working.

Did you take away the “* Time.deltaTime” from line 30 for that reason as well?

Fixed update runs at a constant speed so it serves no purpose.

Ahh right I always forget tiny details like that. Thanks!