Search Unity

Movement, rotate with GUI button. Half working?

Discussion in 'Scripting' started by Afrokid001, Feb 13, 2014.

  1. Afrokid001

    Afrokid001

    Joined:
    Jun 3, 2010
    Posts:
    89
    Hi guys, I am trying to make a simple 2D game but have gotten stumped on a what I thought would be a simple movement code.

    For the movement code, it has 2 buttons, an up and a down button. The hero is always moving forward, and the buttons change the direction.

    Where I am having my trouble is, the down works fine, but the up button instantly raises it to 9.99999 and does nothing else. Is there another way to do this that I am missing?

    I am also having trouble with an "if" state for it to only work from 0 to -90 and 0 to 90 degrees each button so it cant move backwards.

    I thought this would of worked

    Code (csharp):
    1. if (heroObject.Rigidbody2D.rotation <90)
    2. {
    3. }
    but it is saying I'm not referencing to a state of an object (Which I assume is the "Rotation").
    Could I please be pointed in the right direction for this also?

    My code is:


    Code (csharp):
    1.  
    2. var spinSpeed : float = 1; //speed which the object will spin
    3. var forwardVelocityValue : float = 2; //speed the object will move forward
    4. var heroObject : GameObject; //the object this script applies to
    5.  
    6.  
    7. function OnGUI ()
    8. {
    9.     GUI.Box (Rect (10,10,100,90), "Movement"); //draw a box around the buttons
    10.  
    11.     if (GUI.RepeatButton (Rect (20,40,80,20), "Up")) //spin up button
    12.     {
    13.         heroObject.transform.eulerAngles.z= spinSpeed; //Object to spin.Startspinning.angle.z = spin speed
    14.     }
    15.  
    16.     if (GUI.RepeatButton (Rect (20,70,80,20), "Down"))//spin down button
    17.     {
    18.         heroObject.transform.eulerAngles.z-= spinSpeed;//Object to spin.Startspinning.angle.z- = spin speed
    19.     }
    20. }
    21.  
    22.  
    23. function FixedUpdate()
    24. {
    25.     rigidbody2D.velocity = transform.right * forwardVelocityValue;;//object to move.velocity = speed (x,y,)
    26. }
     
  2. Afrokid001

    Afrokid001

    Joined:
    Jun 3, 2010
    Posts:
    89
    I have managed to find a way to get it to work.

    If it helps anyone, feel free to use it or what ever.

    Code (csharp):
    1.  var spinSpeed : float = 1; //speed which the object will spin
    2. var forwardVelocityValue : float = 2; //speed the object will move forward
    3. var heroObject : GameObject; //the object this script applies to
    4. var maximumUpRotation : float = 80;//The maximum rotation for the Up button to work
    5. var minimumUpRotation : float = 275;//The minimum rotation for the Up button to work
    6. var maximumDownRotation : float = 280;//The maximum rotation for the Down button to work
    7. var minimumDownRotation : float = 85;//The minimum rotation for the Down button to work
    8.  
    9.  
    10. function OnGUI ()
    11. {
    12.     GUI.Box (Rect (10,10,100,90), "Movement"); //draw a box around the buttons
    13.  
    14.     if (GUI.RepeatButton (Rect (20,40,80,20), "Up")) //spin up button
    15.     {
    16.         if (transform.localEulerAngles.z < maximumUpRotation) //if rotation is 80 or less then do
    17.         {
    18.             heroObject.transform.Rotate(0,0,spinSpeed);//Object to spin.Startspinning.angle.z
    19.         }
    20.         if (transform.localEulerAngles.z > minimumUpRotation) //if rotation is 255 or more then do
    21.         {
    22.             heroObject.transform.Rotate(0,0,spinSpeed);//Object to spin.Startspinning.angle.z
    23.         }
    24.     }
    25.  
    26.     if (GUI.RepeatButton (Rect (20,70,80,20), "Down"))//spin down button
    27.     {
    28.         if (transform.localEulerAngles.z < minimumDownRotation) //if rotation is 85 or less then do
    29.         {
    30.             heroObject.transform.Rotate(0,0,-spinSpeed);//Object to spin.Startspinning.angle.-z
    31.            
    32.         }
    33.         if (transform.localEulerAngles.z > maximumDownRotation) //if rotation is 250 or more then do
    34.         {
    35.             heroObject.transform.Rotate(0,0,-spinSpeed);//Object to spin.Startspinning.angle.-z
    36.         }
    37.     }
    38. }
    39.  
    40.  
    41. function FixedUpdate()
    42. {
    43.     rigidbody2D.velocity = transform.right * forwardVelocityValue;//object to move.velocity = speed (x,y)
    44. }