Search Unity

Change Speed for Rotatating Character 90 degrees (Isometric view)

Discussion in 'Scripting' started by Troas, Jun 12, 2016.

  1. Troas

    Troas

    Joined:
    Jan 26, 2013
    Posts:
    157
    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:

    1. Single keypress does 90 degree rotation (accomplished)

    2. 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:
    Code (CSharp):
    1.  
    2. privatefloat targetAngle =0;
    3. constfloat rotationAmount =1.5f;
    4. publicfloat rSpeed =1.0f;
    5.  
    6. void Update ()
    7. {
    8. if(Input.GetKeyDown(KeyCode.Q)){targetAngle -=90.0f;}
    9. if(Input.GetKeyDown(KeyCode.E)){targetAngle +=90.0f;}
    10. if(targetAngle !=0){Rotate();}
    11. }
    12.  
    13. protectedvoidRotate()
    14. {
    15.   if(targetAngle>0)
    16.   {
    17.    transform.RotateAround(transform.position,Vector3.up,-rotationAmount);
    18.    targetAngle -= rotationAmount;
    19.   }
    20.   if(targetAngle <0)
    21.   {
    22.    transform.RotateAround(transform.position,Vector3.up, rotationAmount);
    23.    targetAngle += rotationAmount;
    24.   }
    25. }
    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.
     
  2. jimroberts

    jimroberts

    Joined:
    Sep 4, 2014
    Posts:
    560
    Can you describe how it "breaks"? If the rotation never happens it means your rotationAmount is too low.
     
  3. Troas

    Troas

    Joined:
    Jan 26, 2013
    Posts:
    157
    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).
     
  4. jimroberts

    jimroberts

    Joined:
    Sep 4, 2014
    Posts:
    560
    Did you forget to apply deltaTime before adding/subtracting rotationAmount from targetAngle?
     
  5. Troas

    Troas

    Joined:
    Jan 26, 2013
    Posts:
    157

    That's what I tried.

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

    jimroberts

    Joined:
    Sep 4, 2014
    Posts:
    560
    I would personally just do it this way. Change GetKeyDown to GetKey if you want it to keep rotating while the key is pressed.
    Code (CSharp):
    1. public const float RotationDegrees = 90.0f;
    2.  
    3. public float RotationSpeed = 2.5f;
    4. public bool IsRotating = false;
    5. public Quaternion TargetRotation = Quaternion.identity;
    6.  
    7. void Update()
    8. {
    9.     if (!IsRotating)
    10.     {
    11.         if (Input.GetKeyDown(KeyCode.Q))
    12.         {
    13.             TargetRotation = transform.rotation * Quaternion.Euler(0.0f, RotationDegrees, 0.0f);
    14.             IsRotating = true;
    15.         }
    16.         else if (Input.GetKeyDown(KeyCode.E))
    17.         {
    18.             TargetRotation = transform.rotation * Quaternion.Euler(0.0f, -RotationDegrees, 0.0f);
    19.             IsRotating = true;
    20.         }
    21.     }
    22. }
    23.  
    24. void FixedUpdate()
    25. {
    26.     if (IsRotating)
    27.     {
    28.         if (Quaternion.Angle(transform.rotation, TargetRotation) > 0.0f)
    29.         {
    30.             transform.rotation = Quaternion.RotateTowards(transform.rotation, TargetRotation, RotationSpeed);
    31.         }
    32.         else
    33.         {
    34.             IsRotating = false;
    35.         }
    36.     }
    37. }
     
    Last edited: Jun 13, 2016
    Troas likes this.
  7. Troas

    Troas

    Joined:
    Jan 26, 2013
    Posts:
    157
    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!
     
    Last edited: Jun 13, 2016
  8. jimroberts

    jimroberts

    Joined:
    Sep 4, 2014
    Posts:
    560
    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.
     
    Last edited: Jun 13, 2016
  9. Troas

    Troas

    Joined:
    Jan 26, 2013
    Posts:
    157
    Did you take away the "* Time.deltaTime" from line 30 for that reason as well?
     
  10. jimroberts

    jimroberts

    Joined:
    Sep 4, 2014
    Posts:
    560
    Fixed update runs at a constant speed so it serves no purpose.
     
  11. Troas

    Troas

    Joined:
    Jan 26, 2013
    Posts:
    157
    Ahh right I always forget tiny details like that. Thanks!