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.

Basic movement script

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

  1. Afrokid001

    Afrokid001

    Joined:
    Jun 3, 2010
    Posts:
    89
    Hi, I have a basic movement script (The game is a 2D platform style game). The character is continuously swimming forwards, to control it you rotate it up and down to move up and down.

    The problem I am having is, when it rotates, it stops the transform.velocity until it stops rotating, How could I do this more smoothly?

    I have tried both
    transform.Rotate(0,0,2)
    and
    transform.Rotate(Vector3.forward * 2,Space.Self)


    My code is

    Code (csharp):
    1. public static var spinSpeed : float = 1.5; //speed which the object will spin
    2. public static var forwardVelocityValue : float = 4; //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. var upButtonX = Screen.width/4 * 3;
    9.  
    10. var Alive : boolean = true;
    11. var getHealth : PlayerHealth;
    12.  
    13. function Start()
    14. {
    15.     getHealth = GameObject.Find("Player").GetComponent(PlayerHealth);
    16. }
    17.  
    18. function OnGUI ()
    19. {
    20.     if (GUI.RepeatButton (Rect (Screen.width/8*6.5,Screen.height/8*6,Screen.width/100*15,Screen.height/100*20), "Up")) //spin up button
    21.     {
    22.         if (Alive == true)
    23.         {
    24.             if (transform.localEulerAngles.z < maximumUpRotation) //if rotation is 80 or less then do
    25.             {
    26.                 heroObject.transform.Rotate(Vector3.forward * spinSpeed,Space.Self);//Object to spin.Startspinning.angle.z
    27.             }    
    28.             if (transform.localEulerAngles.z > minimumUpRotation) //if rotation is 255 or more then do
    29.             {
    30.                 heroObject.transform.Rotate(0,0,spinSpeed);//Object to spin.Startspinning.angle.z
    31.             }
    32.         }    
    33.     }
    34.  
    35.     if (GUI.RepeatButton (Rect (Screen.width/8*.5,Screen.height/8*6,Screen.width/100*15,Screen.height/100*20), "Down"))//spin down button
    36.     {
    37.         if (Alive == true)
    38.         {
    39.             if (transform.localEulerAngles.z < minimumDownRotation) //if rotation is 85 or less then do
    40.             {
    41.                 heroObject.transform.Rotate(0,0,-spinSpeed);//Object to spin.Startspinning.angle.-z
    42.                
    43.             }
    44.             if (transform.localEulerAngles.z > maximumDownRotation) //if rotation is 250 or more then do
    45.             {
    46.                 heroObject.transform.Rotate(0,0,-spinSpeed);//Object to spin.Startspinning.angle.-z
    47.             }
    48.         }
    49.     }
    50. }
    51.  
    52.  
    53. function FixedUpdate()
    54. {
    55.     if (getHealth.health == 0)
    56.     {
    57.         Alive = false;
    58.     }
    59.     if (Alive == true)
    60.     {
    61.         rigidbody2D.velocity = transform.right * forwardVelocityValue;//object to move.velocity = speed (x,y)
    62.     }
    63.     if (Alive == false)
    64.     {
    65.         rigidbody2D.velocity = transform.up * 2;//object to move.velocity = speed (x,y)
    66.     }
    67.    
    68. }