Search Unity

Why is rotating an 360 degrees over 1 second so difficult

Discussion in 'Scripting' started by ajcombest3, Mar 22, 2018.

  1. ajcombest3

    ajcombest3

    Joined:
    Mar 13, 2018
    Posts:
    27
    The code is not rotating right 360
    It rotates in a chunky fashion each time the BarrelRight(); is called. The rotation amount is far less than 360 as well. I don't get it.
    I just want 1 smooth rotation over 1 second and I do not understand why it doesn't function.

    Code (CSharp):
    1. void Update(){
    2.           if (Input.GetKeyDown(KeyCode.RightArrow)) {
    3.  
    4.             if (buttonPressTimer > 0) {
    5.                 BarrelRight();
    6.             }
    7.             else {
    8.                 buttonPressTimer += 0.5;
    9.                 print(buttonPressTimer);
    10.             }
    11.            
    12.         }
    13. }
    14.     void BarrelRight() {
    15.         transform.Rotate(Vector3.up, 360f * Time.deltaTime / 1f);
    16.     }
     
  2. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
  3. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    It probably has to do with your timer code, which we don't see. Try not using the timer code at all and it should rotate smoothly.
     
    StickyHoneybuns likes this.
  4. ajcombest3

    ajcombest3

    Joined:
    Mar 13, 2018
    Posts:
    27
    My player is kinetic not physics controlled.

    The button timer is just to detect if the button was double tapped or not.
     
  5. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    This code in update will rotate smoothly:
    Code (csharp):
    1.  
    2.  transform.Rotate(Vector3.up, 360f * Time.deltaTime / 1f);
    3.  
    Try it.
     
  6. JoshuaMcKenzie

    JoshuaMcKenzie

    Joined:
    Jun 20, 2015
    Posts:
    916
    You are using GetKeyDown which only fires once per held keypress. use GetKey if you want to rotate each frame while holding the key. Also I don't get what you are doing with buttonPressTimer.