Search Unity

trying to rotate obj fixed degrees once

Discussion in 'Scripting' started by Krodil, Mar 7, 2011.

  1. Krodil

    Krodil

    Joined:
    Jun 30, 2010
    Posts:
    141
    Hi,
    I have now been digging into rotating objs for hours now... no breakthrough

    Basically I want to rotate an obj (a disc) by 180 degrees around x, when I press on it.
    I have a raytrace working and a control setting the disc to true to run my function
    When the disc is true=pressed it turns blue (I did it for visual control of my code) and then it should ONLY rotate once by 180 degrees.
    It turns blue but keeps rotating forever.
    Can't see what's wrong with my for loop.
    My code:
    Code (csharp):
    1.  
    2. var i : int = 0;
    3.  
    4. function IconFeedbackFunction()
    5. {
    6.    
    7.     for(i = 0; i < 20; i ++)
    8.     {
    9.         transform.Rotate(new Vector3(0+i, 0, 0));
    10.         renderer.material.color = Color.blue;
    11.        
    12.        
    13.         //transform.Rotate(Time.deltaTime * damp, Space.World);
    14.         //var target = Vector3(0, 0, 0);
    15.         //var rotate = Quaternion.LookRotation(target - transform.position);
    16.         //transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);
    17.     }
    18. }
    Any pointers?
    the commented lines are some of my attempts.
     
  2. elveatles

    elveatles

    Joined:
    May 2, 2009
    Posts:
    147
    Not sure what the for loop is for. You should probably use Input.GetMouseButtonDown so you know your action only happens once per click. I don't think a boolean is what you need for this. You set it to true, but do you set it to false shortly after? I'm guessing this is why it continues to rotate.
     
  3. Krodil

    Krodil

    Joined:
    Jun 30, 2010
    Posts:
    141
    the loop was to make the obj rotate instead of just setting the value.
    I will have more functionality running when my statement is true, so right now it is needed.
    Pseudo: when something is one, I want the obj to rotate a fixed amount eventhough code might be running from Update()
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    See the Rotation function here.

    --Eric
     
  5. ivkoni

    ivkoni

    Joined:
    Jan 26, 2009
    Posts:
    978
  6. elveatles

    elveatles

    Joined:
    May 2, 2009
    Posts:
    147
    The for loop isn't going to make it rotate like an animation. The for loop all happens on the same frame so any of the in-betweens will not be seen. You could try putting in a yield at the end like in the example Eric5h5 has linked to.
     
  7. Krodil

    Krodil

    Joined:
    Jun 30, 2010
    Posts:
    141
    elveatles: How would you make the for loop then? I also though the syntax was right, but apparently the placing of the function wasn't. :)

    I did not manage to make it work, but found iTween instead. Still annoyed by my lack of skills! :)