Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Rotate Objects and progressive stop Efficiently

Discussion in 'Scripting' started by DanHanfry, Jun 3, 2018.

  1. DanHanfry

    DanHanfry

    Joined:
    Jul 14, 2015
    Posts:
    3
    Good morning, sorry for my English, He tried to rotate a cube, maintain the inertia and stop progressively

    The goal is android

    This code works, but I do not know if it's a good option. If someone can help me improve it

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3.  
    4.  
    5. public class rotate : MonoBehaviour
    6. {
    7.  
    8.     private float rotationSpeed = 15.0F;
    9.     private float descreased = 0.3f;
    10.     float rotX;
    11.     float rotY;
    12.  
    13.     void OnMouseDrag()
    14.     {
    15.  
    16.         rotX = Input.GetAxis("Mouse X") * rotationSpeed * Time.deltaTime;
    17.         rotY = Input.GetAxis("Mouse Y") * rotationSpeed * Time.deltaTime;
    18.  
    19.         transform.Rotate(Vector3.down * rotX, Space.World);
    20.         transform.Rotate(Vector3.right * rotY, Space.World);
    21.  
    22.     }
    23.  
    24.     void Update()
    25.     {
    26.  
    27.        
    28.         if ( Mathf.Round(rotX * 100) != 0 || Mathf.Round(rotY * 100) != 0)
    29.         {
    30.  
    31.             rotX = Mathf.Lerp(rotX, 0f, descreased * Time.deltaTime);
    32.             rotY = Mathf.Lerp(rotY, 0f, descreased * Time.deltaTime);
    33.  
    34.             transform.Rotate(Vector3.down * rotX, Space.World);
    35.             transform.Rotate(Vector3.right * rotY, Space.World);
    36.  
    37.         }
    38.        
    39.  
    40.     }
    41.    
    42.    
    43.  
    44. }
    45.  


     
  2. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    The code is doing what you want and looks to be pretty reasonable. There is nothing there that screams out as being a CPU killer.

    But remember that informed optimisation will come after you have all your code working. Then you can use the profiler to find performance bottlenecks and let the facts guide you in your quest for better performance- rather than my guess work here. :)
     
  3. DanHanfry

    DanHanfry

    Joined:
    Jul 14, 2015
    Posts:
    3
    Thanks, Doug_B all the code of my app is that. It is not for a real case, I seek to learn in depth. I think it could be done with Coroutines and use while to get to 0

    Thanks greetings.
     
  4. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Oh, I see. In that case, there are many ways that you could solve the problem. There are many coding techniques that you could use to gain familiarity with how they work. Coroutines are certainly a good thing to try out and learn.

    Also, you might want to consider having a different structure- for example, what about having an external object control the speed of the cube. How could you go about achieving that?

    Now, what if you wanted to make it so that there are different flavours of external controller. For example, one controller uses keyboard control, another uses mouse drag, another has a simple coded algorithm and yet another uses a UI with buttons that you press.

    How could you make it so that the rotating cube did not care which of these input controllers was controlling it and can continue to function without needing any code changes itself even when you change to a different controller type?

    I'm sure you can think of lots of other things to play around with as well. Have fun. :)
     
    DanHanfry likes this.