Search Unity

How to Rotate + or - 90 degrees each time a button is pressed

Discussion in 'Scripting' started by Darkmonk, Nov 10, 2010.

  1. Darkmonk

    Darkmonk

    Joined:
    Nov 3, 2010
    Posts:
    50
    hi guys,

    bit of a noob question here but im an artist trying to learn to code at the moment so please be gentle with me :p

    I am looking at the code for Mathf.LerpAngle and assume this is the best option to use but this only allows me to rotate between two specified angles, Is there some way i can store the current rotation of my objects X axis + 90 degrees into a variable that i can then use in the code?

    hope that makes sense :)
     
  2. Chris-Sinclair

    Chris-Sinclair

    Joined:
    Jun 14, 2010
    Posts:
    1,326
    maybe:
    float newRotation = myObject.transform.eulerAngles.x + Math.PI / 2;
     
  3. Vicenti

    Vicenti

    Joined:
    Feb 10, 2010
    Posts:
    664
    Euler angles are in degrees, no?

    .x + 90 works fine for me.
     
  4. Chris-Sinclair

    Chris-Sinclair

    Joined:
    Jun 14, 2010
    Posts:
    1,326
    Oh yeah, you're right. Forgot about that. ><

    At work, we got so fed-up with 50% of systems using degrees and 50% using radians that we ended up creating Radians/Degrees wrapper structs with implicit conversions between them. :D
     
  5. Darkmonk

    Darkmonk

    Joined:
    Nov 3, 2010
    Posts:
    50
    great thanks guys :D
     
  6. Darkmonk

    Darkmonk

    Joined:
    Nov 3, 2010
    Posts:
    50
    :( ok it kind of works, the first time i do it the objects rotates 90 degrees but then i want to be able to press the button and have it rotate another 90degrees but it only works once.

    im also having a little trouble with triggereing the full slerp on a button press, i thought i could use Input.GetButton("Fire1") to start the rotation but i need to hold my finger on the button for the rotation to work
    I assume i could just say onbuttondown turn=1 then in an if statement say if turn=1 then Slerp and when the object reaches the desired angle and the change it back to 0. would this be the best way or is there a more efficient way. ?

    here is the part of the code im stuck on.
    Code (csharp):
    1.  
    2. function Update(){
    3.  
    4. if (Input.GetButton("Fire1")){
    5.     TurnRight()
    6. }
    7.  
    8.  
    9. function TurnRight(){
    10.     var baseAngle: float = myObject.transform.eulerAngles.x;
    11.     var newAngle: float = myObject.transform.eulerAngles.x + 90;
    12.     var angle : float = Mathf.LerpAngle(baseAngle, newAngle, Time.time);
    13.     transform.eulerAngles = Vector3(0, angle, 0);
    14. }
    15. }
    thanks
     
  7. Vicenti

    Vicenti

    Joined:
    Feb 10, 2010
    Posts:
    664
    For serious.


    Also.

    To gradually rotate you need to store
    1) That you are or are not rotating
    2) Where you're rotating to (and where from, if necessary)

    Code (csharp):
    1. private var baseAngle : float;
    2. private var newAngle : float;
    3. private var doRotation : boolean;
    4.  
    5. if ( Input.GetButtonDown("Fire1")  !doRotation ) {
    6.   doRotation = true;
    7.   baseAngle = transform.eulerangles.x;
    8.   newAngle = transform.eulerAngles.x + 90;
    9.   rotationEndTime = Time.time + 1.0;
    10. }
    11.  
    12. if ( rotationEndTime > Time.time ) {
    13.   var angle : float = Mathf.LerpAngle(newAngle, baseAngle, rotationEndTime - Time.time );
    14.   transform.eulerAngles.x = angle;
    15. } else {
    16.   doRotation = false;
    17.   transform.eulerAngles.x = newAngle;
    18. }
    You can separate that into a Turn function if you make it a coroutine something like:
    Code (csharp):
    1. function TurnRight() {
    2.   var targetRotation : float = transform.eulerAngles.x + 90;
    3.   var baseRotation : float = transform.eulerAngles.x;
    4.   var timeToTurn : float = 1.0;
    5.   while ( timeToTurn > 0 ) {
    6.     var angle : float = Mathf.LerpAngle( targetRotation, baseRotation, timeToTurn);
    7.     transform.eulerAngles.x = angle;
    8.     timeToTurn -= Time.deltaTime;
    9.     yield;
    10.   }
    11.   transform.eulerAngles.x = targetRotation;
    12. }
     
    Last edited: Nov 10, 2010
  8. Darkmonk

    Darkmonk

    Joined:
    Nov 3, 2010
    Posts:
    50
    wow thanks,

    This works perfectly and after looking over it a few times i understand fully how it works, now i just need to get my brain into that kind of thinking for any future problems :D

    Thank you for taking the time to write this for me, much apprieciated.