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. Dismiss Notice

Help to rotate stop rotate

Discussion in 'Scripting' started by LazyDog_Rich, Feb 4, 2015.

  1. LazyDog_Rich

    LazyDog_Rich

    Joined:
    Apr 27, 2014
    Posts:
    70
    hi everyone. Im trying to figuere out how to make something rotate on himself, and stop at 90º then wait for some seconds and start to rotate over again.
    Im new in C# I used to write on Js.

    I was thinkings in something simple like:

    transform.Rotate (new Vector3 (0, 0, 1)*speed, Space.Self);

    with something like this, I can make the object to rotate. But I dont know how to stop it in the 90º or even how to make it wat some seconds before start to rotating again since "yield" work pretty different in C#

    Im kinda lost right now :c I would really appreciate some help
     
  2. meatpudding

    meatpudding

    Joined:
    Jan 28, 2015
    Posts:
    39
    I think yo could do everything you need to in the update method. This should be a starting point.
    Code (csharp):
    1. float startTime;
    2. float endTime;
    3. float startAngle = 0.0f;
    4. float endAngle = 90.0f;
    5.  
    6. void Start() {
    7.   startTime = Time.time + 5.0f;
    8.   endTime = Time.time + 10.0f;
    9. }
    10.  
    11. void Update() {
    12.   // Set the angle
    13.   if (Time.time > startTime) {
    14.     float t = (Time.time - startTime) / (endTime - startTime)
    15.     float rotate = Mathf.LerpAngle(startAngle, endAngle, t);
    16.     transform.localEulerAngles = new Vector3(0.0f, rotate, 0.0f);
    17.   }
    18.  
    19.   // Finish and get ready for next rotation
    20.   if (Time.time > endTime) {
    21.     startTime = Time.time + 5.0f;
    22.     endTime = Time.time + 10.0f;
    23.   }
    24. }
    25.  
     
    LazyDog_Rich likes this.
  3. LazyDog_Rich

    LazyDog_Rich

    Joined:
    Apr 27, 2014
    Posts:
    70
    Tha
    Thanks, just now I was writting somthing similar, but what you give me help me to improve it. When finish and working I'll post here so you can let'me what you think.

    Thank you!
     
  4. LazyDog_Rich

    LazyDog_Rich

    Joined:
    Apr 27, 2014
    Posts:
    70
    Finnally I got it working:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class BigGear : MonoBehaviour
    5. {
    6.     public float startTime;
    7.     public float endTime;
    8.    
    9.     void Start()
    10.         {
    11.         startTime = Time.time + 0.0f;
    12.         endTime = Time.time + 3.0f;
    13.         }
    14.    
    15.     void Update()
    16.         {
    17.         if (Time.time > startTime)
    18.             {
    19.             transform.Rotate (new Vector3 (0, 0, 1)*4, Space.Self);
    20.             }
    21.         // Finish and get ready for next rotation
    22.         if (Time.time > endTime)
    23.             {
    24.             startTime = Time.time + 1.0f;
    25.             endTime = Time.time + 4.0f;
    26.             transform.localEulerAngles = new Vector3(0.0f, 0.0f, 12);
    27.             }
    28.         }
    29. }
    Thanks a lot srsly! :D