Search Unity

Side scrolling space game

Discussion in 'Scripting' started by Afrokid001, Feb 15, 2017.

  1. Afrokid001

    Afrokid001

    Joined:
    Jun 3, 2010
    Posts:
    89
    I'm trying to make a script that rotates a moving ship up and down when one of two buttons are pressed.

    The code I have done works but there is a noticeable stutter when rotating, like it would rotate, move, rotate, move, rotate and move instead of a nice fluid turn.

    The code I am using is
    Code (csharp):
    1.  
    2.  void FixedUpdate()
    3.     {
    4.         if (getHealth.isAlive == true)
    5.         {
    6.             GetComponent<Rigidbody2D>().velocity = transform.right * forwardVelocityValue;//object to move.velocity = speed (x,y)
    7.         }
    8.         if (getHealth.isAlive == false)
    9.         {
    10.             GetComponent<Rigidbody2D>().velocity = transform.up * 2;//object to move.velocity = speed (x,y)
    11.         }
    12.         if (isRotatingUp == true)
    13.         {
    14.             if (transform.localEulerAngles.z < maximumUpRotation) //if rotation is 80 or less then do
    15.             {
    16.                 heroObject.transform.Rotate(0, 0, spinSpeed);//Object to spin.Startspinning.angle.z
    17.             }
    18.             if (transform.localEulerAngles.z > minimumUpRotation) //if rotation is 255 or more then do
    19.             {
    20.                 heroObject.transform.Rotate(0, 0, spinSpeed);//Object to spin.Startspinning.angle.z
    21.             }
    22.             isRotatingUp = false;
    23.         }
    24.         if (isRotatingDown == true)
    25.         {
    26.             if (getHealth.isAlive == true)
    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.             isRotatingDown = false;
    39.         }
    40.  

    so what I am trying to do is make a nice fluid constantly moving ship (no control over the forward but gets faster the further you go) that when dies stops and just slowly moves up.

    Any ideas how I could solve this issue?

    Thanks.
     
  2. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
    1st problem is you need to cache the rigidbody so you don't keep calling the expensive GetComponent method every fixedUpdate. Move the GetComponent to Start/Awake/OnEnable so it only does it once and keeps a reference around

    Code (CSharp):
    1. public class MyClass : MonoBehaviour
    2. {
    3.     RigidBody2D myRigidBody;
    4.  
    5.     void Awake()
    6.     {
    7.         myRigidBody = GetComponent<RigidBody2D>();
    8.     }
    9. }
    2nd problem is your if statements need some adjusting so the logic suits what you want to do.

    Code (CSharp):
    1. void FixedUpdate()
    2. {
    3.     if (getHealth.isAlive)//No need for == true
    4.     {
    5.         myRigidBody.velocity = transform.right * forwardVelocityValue;
    6.         if (isRotatingUp)
    7.         {
    8.             heroObject.transform.Rotate(0, 0, spinSpeed);
    9.         }
    10.         else if (isRotatingDown)
    11.         {
    12.             heroObject.transform.Rotate(0, 0, -spinSpeed);
    13.         }
    14.     }
    15.     else //Same as == false
    16.     {
    17.         myRigidBody.velocity = transform.up * 2;
    18.     }
    19. }
    Try this and see if it helps. I see other potential issues, but I need to see how you are setting isRotatingUp and isRotatingDown.
     
  3. Afrokid001

    Afrokid001

    Joined:
    Jun 3, 2010
    Posts:
    89
    This is my original code. The reasoning behind the isRotating is to stop the rotation when I release the button.

    when I use this new code, it just rotates on itself while moving forward, my original code makes the guy follow the direction he is facing.

    Code (csharp):
    1.  
    2. public class Movement : MonoBehaviour
    3. {
    4.     public static float spinSpeed = 2;
    5.     public static int forwardVelocityValue = 4;
    6.     public GameObject heroObject;
    7.     private int maximumUpRotation = 80;
    8.     private int minimumUpRotation = 275;
    9.     private int maximumDownRotation = 280;
    10.     private int minimumDownRotation = 85;
    11.     bool isRotatingUp = false;
    12.     bool isRotatingDown = false;
    13.  
    14.     public Texture2D rotateUpImage;
    15.     public Texture2D rotateDownImage;
    16.     public GUISkin mySkin;
    17.  
    18.  
    19.     void OnGUI()
    20.     {
    21.         GUI.skin = mySkin;
    22.         PlayerHealth getHealth = GetComponent<PlayerHealth>();
    23.         if (GUI.RepeatButton(new Rect(Screen.width / 8 * 6.5f, Screen.height / 8 * 6, Screen.width / 7, Screen.height / 5), rotateUpImage)) //spin up button
    24.         {
    25.             if (getHealth.fishAlive == true)
    26.             {
    27.                 isRotatingUp = true;
    28.             }
    29.         }
    30.  
    31.         if (GUI.RepeatButton(new Rect(Screen.width / 8 * 0.5f, Screen.height / 8 * 6, Screen.width / 7, Screen.height / 5), rotateDownImage))
    32.         {
    33.             if (getHealth.fishAlive == true)
    34.             {
    35.                 isRotatingDown = true;
    36.             }
    37.         }
    38.     }
    39.     void FixedUpdate()
    40.     {
    41.         PlayerHealth getHealth = GetComponent<PlayerHealth>();
    42.         if (getHealth.fishAlive == true)
    43.         {
    44.             GetComponent<Rigidbody2D>().velocity = transform.right * forwardVelocityValue;//object to move.velocity = speed (x,y)
    45.         }
    46.         if (getHealth.fishAlive == false)
    47.         {
    48.             GetComponent<Rigidbody2D>().velocity = transform.up * 2;//object to move.velocity = speed (x,y)
    49.         }
    50.         if (isRotatingUp == true)
    51.         {
    52.             if (transform.localEulerAngles.z < maximumUpRotation) //if rotation is 80 or less then do
    53.             {
    54.                 heroObject.transform.Rotate(0, 0, spinSpeed);//Object to spin.Startspinning.angle.z
    55.             }
    56.             if (transform.localEulerAngles.z > minimumUpRotation) //if rotation is 255 or more then do
    57.             {
    58.                 heroObject.transform.Rotate(0, 0, spinSpeed);//Object to spin.Startspinning.angle.z
    59.             }
    60.             isRotatingUp = false;
    61.         }
    62.         if (isRotatingDown == true)
    63.         {
    64.             if (getHealth.fishAlive == true)
    65.             {
    66.                 if (transform.localEulerAngles.z < minimumDownRotation) //if rotation is 85 or less then do
    67.                 {
    68.                     heroObject.transform.Rotate(0, 0, -spinSpeed);//Object to spin.Startspinning.angle.-z
    69.  
    70.                 }
    71.                 if (transform.localEulerAngles.z > maximumDownRotation) //if rotation is 250 or more then do
    72.                 {
    73.                     heroObject.transform.Rotate(0, 0, -spinSpeed);//Object to spin.Startspinning.angle.-z
    74.                 }
    75.             }
    76.             isRotatingDown = false;
    77.         }
    78.         if (Score.distance == 10)
    79.         {
    80.             forwardVelocityValue = 5;//object to move.velocity = speed (x,y)
    81.         }
    82.         if (Score.distance == 50)
    83.         {
    84.             forwardVelocityValue = 6;//object to move.velocity = speed (x,y)
    85.         }
    86.         if (Score.distance == 100)
    87.         {
    88.             forwardVelocityValue = 7;//object to move.velocity = speed (x,y)
    89.         }
    90.         if (Score.distance == 200)
    91.         {
    92.             forwardVelocityValue = 8;//object to move.velocity = speed (x,y)
    93.         }
    94.         if (Score.distance == 250)
    95.         {
    96.             forwardVelocityValue = 9;//object to move.velocity = speed (x,y)
    97.         }
    98.         if (Score.distance == 300)
    99.         {
    100.             forwardVelocityValue = 10;//object to move.velocity = speed (x,y)
    101.         }
    102.         if (Score.distance == 450)
    103.         {
    104.             forwardVelocityValue = 11;//object to move.velocity = speed (x,y)
    105.         }
    106.         if (Score.distance == 500)
    107.         {
    108.             forwardVelocityValue = 12;//object to move.velocity = speed (x,y)
    109.         }
    110.         if (Score.distance == 650)
    111.         {
    112.             forwardVelocityValue = 13;//object to move.velocity = speed (x,y)
    113.         }
    114.         if (Score.distance == 800)
    115.         {
    116.             forwardVelocityValue = 14;//object to move.velocity = speed (x,y)
    117.         }
    118.     }
    119.     void Update()
    120.     {
    121.         if (Input.GetKey("down"))
    122.         {
    123.             isRotatingDown = true;
    124.         }
    125.         if (Input.GetKey("up"))
    126.         {
    127.             isRotatingUp = true;
    128.         }
    129.     }
    130. }
    131.  
     
  4. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
    Yeah I forgot to put the isRotatiingUp = false; and isRotatingDown = false; where it rotates it.

    Code (CSharp):
    1. if (isRotatingUp)
    2. {
    3.     heroObject.transform.Rotate(0, 0, spinSpeed);
    4.     isRotatingUp= false;
    5. }
    6. else if (isRotatingDown)
    7. {
    8.     heroObject.transform.Rotate(0, 0, -spinSpeed);
    9.     isRotatingDown= false;
    10. }
     
  5. Afrokid001

    Afrokid001

    Joined:
    Jun 3, 2010
    Posts:
    89
    I have uploaded this video to show what I am referring to.
    watch closely when he is rotating as it seems to jitter or shake.

     
  6. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
    2 things that it could be are.

    - OnGUI() isn't really supposed to be used for game play anymore. Try using something else like the new UI buttons or even Input.GetKey() in update to test it in the editor.
    - rotating via the transform might not work as expected when you are using rigidbody. Try using the rigidbody to rotate it.
     
  7. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @Afrokid001

    Hi there,

    I don't see Time.deltatime anywhere in this thread.

    You should (always) take frame time into account in your rotations and movements.
     
  8. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
    They are using FixedUpdate so not really needed to have Time.deltaTime or even Time.fixedDeltaTime.
    If it was Update then that would have been my first suggestion as it is frame dependent.