Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

how to rotate game object anti-clockwise as (-Time.deltaTime) does not work

Discussion in 'Scripting' started by sarangge, Sep 7, 2015.

  1. sarangge

    sarangge

    Joined:
    Jul 29, 2015
    Posts:
    11
    I want the object to keep on moving by every frame (using the Time.deltaTime) by clockwise & anti-clockwise rotation.

    The player will put the angle they want the object to rotate. Lets say, the angle is 70. After the object is rotate to 70 degree, then it should rotate -70 degree.
    Code (CSharp):
    1. public class Rotate : MonoBehaviour {
    2.    
    3.     public enum Direction
    4.     { NONE,RIGHT, LEFT,FORWARD,BACKWARD,DOWN,UP };
    5.    
    6.     public bool Enabled=false;
    7.  
    8.     public GameObject kiub;
    9.    
    10.     [System.Serializable]
    11.     public class Rotation {
    12.        
    13.         public bool RotateEnabled = false;
    14.         public Direction RotateDirection;
    15.         public float RotateSpeed=10;
    16.         public float Angle;
    17.     }
    18.     public Rotation ObjectRotation;
    19.  
    20.     [System.Serializable]
    21.     public class Movement {
    22.         public bool MovementEnabled = false;
    23.         public Vector3 MovementAxis;
    24.         public float MovementUnit=0.0001f;
    25.     }
    26.     public Movement ObjectMovement;
    27.  
    28.     void Update () {
    29.        
    30.         if (Enabled) {
    31.             if (ObjectRotation.RotateEnabled) {
    32.                 if (ObjectRotation.RotateDirection == Direction.UP) {
    33.                     if(kiub.transform.rotation.eulerAngles.y <= ObjectRotation.Angle)
    34.                     {
    35.                         transform.Rotate (Vector3.up * Time.deltaTime * ObjectRotation.RotateSpeed);
    36.                     }
    37.                     if(kiub.transform.rotation.eulerAngles.y >= (ObjectRotation.Angle + 0.001))
    38.                     {
    39.                         transform.Rotate (Vector3.up * (-Time.deltaTime) * ObjectRotation.RotateSpeed);
    40.                     }
    41.                 }
    42.             }
    43.         }
    44.     }
    45. }
    46.  
    So far,
    Code (CSharp):
    1. transform.Rotate (Vector3.up * Time.deltaTime * ObjectRotation.RotateSpeed);
    does work. and it work perfectly the way i want it. BUT, when i want it to rotate the other way around by using
    Code (CSharp):
    1. transform.Rotate (Vector3.up * (-Time.deltaTime) * ObjectRotation.RotateSpeed);
    , it only rotate to 359 only. no matter how much the value of the ObjectRotation.Angle is.


    Please help me for this. Thank you so much. i really dont have any idea why it happen that way, and how to fix it. Please. Thank you.


    By the way, i get the idea of (-Time.deltaTime) from http://johnstejskal.com/wp/rotating-objects-and-sprites-in-unity3d-using-cs-c/
     
  2. CaptainKirby

    CaptainKirby

    Joined:
    Dec 12, 2011
    Posts:
    36
    Not sure if it changes anything but instead of doing -Time.deltaTime, try -Vector3.up, which is the actual direction?
     
    Slashik likes this.
  3. sarangge

    sarangge

    Joined:
    Jul 29, 2015
    Posts:
    11
    erm..no. it does not work. it still rotate to 359 only. :(
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    store the forward vector at the start of the rotation, check the angle between the current rotation and that stored value.

    you can't really do direct comparisons with euler values as you have in the if. 0, 360, -360, 720, -720... all the same angle, how is that going to evaluate with "<="...
     
  5. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    Rotation works a bit funky in Unity which is why you can't use -Time.deltatime.

    In the following tutorial series there is talk about a possible solution for your problem, though in his case it is for a fps camera, but I am sure the same principles apply: https://www.youtube.com/playlist?list=PLbghT7MmckI4su9oKnoLuoRYLUvKlek7j

    I believe it is either part 2 or part 3 where he explains it. I'd look it up myself, but am currently at work so I can't take the time to sift through the videos, sorry.
     
  6. Lentaq

    Lentaq

    Joined:
    Apr 8, 2015
    Posts:
    57
    Couldn't you just put a * -1 in your equation at instead of trying to do -Time? (Time.deltaTime * -1)
     
  7. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Why would that solve anything?
     
  8. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    I went over the tutorials, I think this is the part that will be interesting for you (starts at ~15:40)