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 use Time.deltaTime when specifically rotating only 90 degrees

Discussion in 'Scripting' started by JacksonFrew, Oct 10, 2015.

  1. JacksonFrew

    JacksonFrew

    Joined:
    Aug 13, 2015
    Posts:
    3
    Hi all,

    Currently I have an object that after I provide input it rotates 90 degrees in different ways on a global rotation. Unfortunately when I moved it over to iOS I realised that I had not implemented a Time.deltaTime multiplier, so the rotation was slower than it should have been.

    Here is the code, its probably terrible.

    Code (CSharp):
    1. public class CubeController : MonoBehaviour {
    2.  
    3.     public GameObject cube = null;
    4.  
    5.     public float xRot = 0f;
    6.     public float yRot = 0f;
    7.     public float zRot = 0f;
    8.  
    9.     public bool imRotating = false;
    10.  
    11.     public int speed = 0;
    12.  
    13.     //Use this for initialization
    14.     void Start () {
    15.  
    16.         GameObject.Find("SwipeController").GetComponent<SwipeControl>().SetMethodToCall(MyCallbackMethod);
    17.  
    18.     }
    19.  
    20.     //Update is called once per frame
    21.     void Update() {
    22.  
    23.         Transform theCube = cube.GetComponent<Transform>();
    24.  
    25.         if (xRot != 0 || yRot != 0 || zRot != 0)
    26.         {
    27.             imRotating = true;
    28.         }
    29.         else
    30.         {
    31.             imRotating = false;
    32.         }
    33.  
    34.      
    35.  
    36.         //xRot
    37.         if (xRot > 0)
    38.         {
    39.             theCube.Rotate(-speed, 0, 0, Space.World);
    40.             xRot -= speed;
    41.         }
    42.  
    43.         if (xRot < 0)
    44.         {
    45.             theCube.Rotate(speed, 0, 0, Space.World);
    46.             xRot += speed;
    47.         }
    48.  
    49.         //zRot
    50.         if (zRot > 0)
    51.         {
    52.             theCube.Rotate(0, 0, -speed, Space.World);
    53.             zRot -= speed;
    54.         }
    55.  
    56.         if (zRot < 0)
    57.         {
    58.             theCube.Rotate(0, 0, speed, Space.World);
    59.             zRot += speed;
    60.         }
    61.  
    62.         //yRot
    63.         if (yRot > 0)
    64.         {
    65.             theCube.Rotate(0, -speed, 0, Space.World);
    66.             yRot -= speed;
    67.         }
    68.  
    69.         if (yRot < 0)
    70.         {
    71.             theCube.Rotate(0, speed, 0, Space.World);
    72.             yRot+=speed;
    73.         }
    74.  
    75.      
    76.  
    77.     }
    78.  
    79.     public void RotateTopLeft()
    80.     {
    81.         if (imRotating == false)
    82.         {
    83.             zRot = 90f;
    84.         }
    85.         else
    86.         {
    87.             print("I can't rotate, I'm still rotating");
    88.         }
    89.     }
    90.  
    91.     public void RotateTopRight()
    92.     {
    93.         if (imRotating == false)
    94.         {
    95.             zRot = -90f;
    96.         }
    97.         else
    98.         {
    99.             print("I can't rotate, I'm still rotating");
    100.         }
    101.     }
    102.  
    103.     public void RotateBottomLeft()
    104.     {
    105.         if (imRotating == false)
    106.         {
    107.             xRot = -90f;
    108.         }
    109.         else
    110.         {
    111.             print("I can't rotate, I'm still rotating");
    112.         }
    113.     }
    114.  
    115.     public void RotateBottomRight()
    116.     {
    117.         if (imRotating == false)
    118.         {
    119.             xRot = 90f;
    120.         }
    121.         else
    122.         {
    123.             print("I can't rotate, I'm still rotating");
    124.         }
    125.     }
    In summary, I need the object to rotate to 90 degrees on world rotation on different axis, but I need the speed of the rotation to be consistent over different frame rates. I've tried multiplying the speed by Time.deltaTime but it doesn't seem to work with my script. Any suggestions?

    Cheers.
     
    Last edited: Oct 10, 2015
  2. Thaao

    Thaao

    Joined:
    Jun 9, 2014
    Posts:
    54
  3. JacksonFrew

    JacksonFrew

    Joined:
    Aug 13, 2015
    Posts:
    3
    Does quaternion support world rotation though?
     
  4. Thaao

    Thaao

    Joined:
    Jun 9, 2014
    Posts:
    54
    Well, Slerp just interpolates between two rotations. So you'd just have to make sure the rotation you turn toward is relative, which... seems like a lot of work, but there's probably an easy way to do it. If it's working how you want with Transform.Rotate using Time.deltaTime, there's no reason to change it. I just was very happy to learn Slerp lol.
     
  5. JacksonFrew

    JacksonFrew

    Joined:
    Aug 13, 2015
    Posts:
    3
    Yeah, issue is say I used this:
    Code (CSharp):
    1. transform.Rotate(Vector3.up * Time.deltaTime, Space.World);
    Because it's no longer attached to the xRot/zRot/yRot variable in my code before, I'm unsure of how to get it to stop on exactly 90 degrees past its last position.
     
  6. Thaao

    Thaao

    Joined:
    Jun 9, 2014
    Posts:
    54
    Have you tried just multiplying your "speed" variable by Time.deltaTime every time you use it...?

    As for how to get it to stop on exact 90s, I'm not sure how you're accomplishing that now. Is "speed" always set to a factor of 90 so that it will always force xRot/zRot/yRot to end on 90 eventually?

    You'll probably have to add and change a bit of code, to check that they've reached at least the target, then set them to exactly the target (since it will almost never land on an exact number).
     
    Cepunto likes this.