Side scrolling space game

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

 void FixedUpdate()
    {
        if (getHealth.isAlive == true)
        {
            GetComponent<Rigidbody2D>().velocity = transform.right * forwardVelocityValue;//object to move.velocity = speed (x,y)
        }
        if (getHealth.isAlive == false)
        {
            GetComponent<Rigidbody2D>().velocity = transform.up * 2;//object to move.velocity = speed (x,y)
        }
        if (isRotatingUp == true)
        {
            if (transform.localEulerAngles.z < maximumUpRotation) //if rotation is 80 or less then do
            {
                heroObject.transform.Rotate(0, 0, spinSpeed);//Object to spin.Startspinning.angle.z
            }
            if (transform.localEulerAngles.z > minimumUpRotation) //if rotation is 255 or more then do
            {
                heroObject.transform.Rotate(0, 0, spinSpeed);//Object to spin.Startspinning.angle.z
            }
            isRotatingUp = false;
        }
        if (isRotatingDown == true)
        {
            if (getHealth.isAlive == true)
            {
                if (transform.localEulerAngles.z < minimumDownRotation) //if rotation is 85 or less then do
                {
                    heroObject.transform.Rotate(0, 0, -spinSpeed);//Object to spin.Startspinning.angle.-z

                }
                if (transform.localEulerAngles.z > maximumDownRotation) //if rotation is 250 or more then do
                {
                    heroObject.transform.Rotate(0, 0, -spinSpeed);//Object to spin.Startspinning.angle.-z
                }
            }
            isRotatingDown = false;
        }

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.

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

public class MyClass : MonoBehaviour
{
    RigidBody2D myRigidBody;

    void Awake()
    {
        myRigidBody = GetComponent<RigidBody2D>();
    }
}

2nd problem is your if statements need some adjusting so the logic suits what you want to do.

void FixedUpdate()
{
    if (getHealth.isAlive)//No need for == true
    {
        myRigidBody.velocity = transform.right * forwardVelocityValue;
        if (isRotatingUp)
        {
            heroObject.transform.Rotate(0, 0, spinSpeed);
        }
        else if (isRotatingDown)
        {
            heroObject.transform.Rotate(0, 0, -spinSpeed);
        }
    }
    else //Same as == false
    {
        myRigidBody.velocity = transform.up * 2;
    }
}

Try this and see if it helps. I see other potential issues, but I need to see how you are setting isRotatingUp and isRotatingDown.

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.

public class Movement : MonoBehaviour
{
    public static float spinSpeed = 2;
    public static int forwardVelocityValue = 4;
    public GameObject heroObject;
    private int maximumUpRotation = 80;
    private int minimumUpRotation = 275;
    private int maximumDownRotation = 280;
    private int minimumDownRotation = 85;
    bool isRotatingUp = false;
    bool isRotatingDown = false;

    public Texture2D rotateUpImage;
    public Texture2D rotateDownImage;
    public GUISkin mySkin;


    void OnGUI()
    {
        GUI.skin = mySkin;
        PlayerHealth getHealth = GetComponent<PlayerHealth>();
        if (GUI.RepeatButton(new Rect(Screen.width / 8 * 6.5f, Screen.height / 8 * 6, Screen.width / 7, Screen.height / 5), rotateUpImage)) //spin up button
        {
            if (getHealth.fishAlive == true)
            {
                isRotatingUp = true;
            }
        }

        if (GUI.RepeatButton(new Rect(Screen.width / 8 * 0.5f, Screen.height / 8 * 6, Screen.width / 7, Screen.height / 5), rotateDownImage))
        {
            if (getHealth.fishAlive == true)
            {
                isRotatingDown = true;
            }
        }
    }
    void FixedUpdate()
    {
        PlayerHealth getHealth = GetComponent<PlayerHealth>();
        if (getHealth.fishAlive == true)
        {
            GetComponent<Rigidbody2D>().velocity = transform.right * forwardVelocityValue;//object to move.velocity = speed (x,y)
        }
        if (getHealth.fishAlive == false)
        {
            GetComponent<Rigidbody2D>().velocity = transform.up * 2;//object to move.velocity = speed (x,y)
        }
        if (isRotatingUp == true)
        {
            if (transform.localEulerAngles.z < maximumUpRotation) //if rotation is 80 or less then do
            {
                heroObject.transform.Rotate(0, 0, spinSpeed);//Object to spin.Startspinning.angle.z
            }
            if (transform.localEulerAngles.z > minimumUpRotation) //if rotation is 255 or more then do
            {
                heroObject.transform.Rotate(0, 0, spinSpeed);//Object to spin.Startspinning.angle.z
            }
            isRotatingUp = false;
        }
        if (isRotatingDown == true)
        {
            if (getHealth.fishAlive == true)
            {
                if (transform.localEulerAngles.z < minimumDownRotation) //if rotation is 85 or less then do
                {
                    heroObject.transform.Rotate(0, 0, -spinSpeed);//Object to spin.Startspinning.angle.-z

                }
                if (transform.localEulerAngles.z > maximumDownRotation) //if rotation is 250 or more then do
                {
                    heroObject.transform.Rotate(0, 0, -spinSpeed);//Object to spin.Startspinning.angle.-z
                }
            }
            isRotatingDown = false;
        }
        if (Score.distance == 10)
        {
            forwardVelocityValue = 5;//object to move.velocity = speed (x,y)
        }
        if (Score.distance == 50)
        {
            forwardVelocityValue = 6;//object to move.velocity = speed (x,y)
        }
        if (Score.distance == 100)
        {
            forwardVelocityValue = 7;//object to move.velocity = speed (x,y)
        }
        if (Score.distance == 200)
        {
            forwardVelocityValue = 8;//object to move.velocity = speed (x,y)
        }
        if (Score.distance == 250)
        {
            forwardVelocityValue = 9;//object to move.velocity = speed (x,y)
        }
        if (Score.distance == 300)
        {
            forwardVelocityValue = 10;//object to move.velocity = speed (x,y)
        }
        if (Score.distance == 450)
        {
            forwardVelocityValue = 11;//object to move.velocity = speed (x,y)
        }
        if (Score.distance == 500)
        {
            forwardVelocityValue = 12;//object to move.velocity = speed (x,y)
        }
        if (Score.distance == 650)
        {
            forwardVelocityValue = 13;//object to move.velocity = speed (x,y)
        }
        if (Score.distance == 800)
        {
            forwardVelocityValue = 14;//object to move.velocity = speed (x,y)
        }
    }
    void Update()
    {
        if (Input.GetKey("down"))
        {
            isRotatingDown = true;
        }
        if (Input.GetKey("up"))
        {
            isRotatingUp = true;
        }
    }
}

Yeah I forgot to put the isRotatiingUp = false; and isRotatingDown = false; where it rotates it.

if (isRotatingUp)
{
    heroObject.transform.Rotate(0, 0, spinSpeed);
    isRotatingUp= false;
}
else if (isRotatingDown)
{
    heroObject.transform.Rotate(0, 0, -spinSpeed);
    isRotatingDown= false;
}

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.

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.

@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.

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.