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

how can i rotate an object through a script?

Discussion in 'Scripting' started by Quist, Sep 25, 2014.

  1. Quist

    Quist

    Joined:
    Feb 25, 2014
    Posts:
    284
    So i have a Closet, and the closet have 2 children attached to it, left & right site of the closet to be opened.

    So i want a script which will make the left closet side open with -90 degress in the X axis.
    And the right closet side should open with +90 degress in the X axis.

    i have tried making one but it instantly goes to -90 & +90 and i want it to transition smoothly over to it, like it should take 1.5 seconds to get to -90 & +90.

    Long story short: I need something added to the following script linked below which will make it rotate over time instead of instantly going to 90 degress! ^^

    Script:
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var theLeftCloset : Transform;
    4. var theRightCloset : Transform;
    5. var closetIsClosed = true;
    6. private var drawGUI = false;
    7.  
    8. function Update ()
    9. {
    10.     if (drawGUI == true && Input.GetKeyDown("mouse 0") && closetIsClosed == false)
    11.     {
    12.         theLeftCloset.transform.Rotate(0, -90, 0);
    13.         theRightCloset.transform.Rotate(0, 90, 0);
    14.         closetIsClosed = true;
    15.     }
    16.     else if (drawGUI == true && closetIsClosed == true && Input.GetKeyDown("mouse 0"))
    17.     {
    18.         theLeftCloset.transform.Rotate(0, 90, 0);
    19.         theRightCloset.transform.Rotate(0, -90, 0);
    20.         closetIsClosed = false;
    21.     }
    22. }
    23.  
    24. function OnTriggerEnter (theCollider : Collider)
    25. {
    26.     if (theCollider.tag == "Player")
    27.     {
    28.         drawGUI = true;
    29.     }
    30. }
    31.  
    32. function OnTriggerExit (theCollider : Collider)
    33. {
    34.     if (theCollider.tag == "Player")
    35.     {
    36.         drawGUI = false;
    37.     }
    38. }
    39.  
    40. function OnGUI ()
    41. {
    42.     if (drawGUI == true)
    43.     {
    44.         GUI.Box (Rect (Screen.width*0.5-50, Screen.height*0.5-25, 170, 22), "Left click to open the door");
    45.     }
    46. }
     
  2. Redtail87

    Redtail87

    Joined:
    Jul 17, 2013
    Posts:
    124
  3. Quist

    Quist

    Joined:
    Feb 25, 2014
    Posts:
    284
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    There's an example of that here. (Note: to make your life easy, you want RotateTowards, not slerp.)