Unity Course Rocket Game - issue

Hello there,

I’m doing the Unity Course now and am at the stage of creating the Rocket Ship game.
I noticed one issue: when I play the game (in Unity) while the Rocket Ship has been selected in the hierarchy window, then the Rocket does move according to all the settings I’d chosen (mass, drag etc.). If, however, another asset has been highlighted before I hit play, then the Rocket goes way too fast as if the settings were completely different. The problem occurs each time I play - even if I make sure that the Rocket has been selected before playing, when I move to Level 2 it invariably ‘looses’ all its settings and goes way to fast.

Would any of you have a solution to this?
Thanks for your help.

Hi Uriaa,

There is a strange bug in Unity which prevents the rocket from flying when it is selected in the Hierarchy. Try to select something else and click into the game window once to set Unity’s focus on the game window again.

If the rocket does not move properly, try to remove Time.deltaTime from the AddRelativeForce method. Alternatively, test an alternative solution in the Rocket.cs:

bool isThrusting = false;

private void RespondToThrustInput()
{
    isThrusting = Input.GetKey(KeyCode.Space);

    if (isThrusting)
    {
        ApplyThrust();
    }
    else
    {
        audioSource.Stop();
        mainEngineParticles.Stop();
    }
}

private void ApplyThrust()
{
    if (!audioSource.isPlaying)
    {
        audioSource.PlayOneShot(mainEngine);
    }

    mainEngineParticles.Play();
}

private void FixedUpdate()
{
    if (isThrusting)
    {
        rigidBody.AddRelativeForce(Vector3.up * mainThrust, ForceMode.Acceleration);
    }
}

See this modified script.

Did this fix the issue?

Hi Nina,

Thanks a lot for your reply.
I’ve tried removing Time.deltaTime (figuring out how to use the script you shared will take me some time;))
However, the problem you described seems to be different from the one I have - this is what happens in my case:
a) when I select the Rocket in the Hierarchy and then play, the Rocket flies slowly - exactly according to the mass, drag and Rcs thrust settings I put.
b) when another element is selected or if nothing is selected in Hierarchy and then I play, the Rocket flies with much more speed (as if the mass, drag and thrust had lower values). The difference in speed is quite considerable.
Any idea what the issue might be?
Thanks!

PS. Tried the modified script but it doesn’t load in Unity - the error message asks to fix compile errors though there is none.

VERY likely the bug I described in my first answer. It has been around for years. Neither you nor we can fix it because it seems to be a bug in the engine. :frowning:

Could you share your updated script as formatted text, please? And what errors do you get?

using System;
using UnityEngine;
using UnityEngine.SceneManagement;
public class rocket : MonoBehaviour
{
    // Control cohete
    [SerializeField] float rcsThrust = 100f;
    [SerializeField] float mainThrust = 100f;
    [SerializeField] float levelLoadDelay = 2f;
    //Sonidos cohete
    [SerializeField] AudioClip mainEngine;
    [SerializeField] AudioClip death;
    [SerializeField] AudioClip sucess;
    //Particulas cohete
    [SerializeField] ParticleSystem mainEngineParticles;
    [SerializeField] ParticleSystem deathParticles;
    [SerializeField] ParticleSystem sucessParticles;
    Rigidbody rigidBody;
    AudioSource audioSource;
    enum State { Alive, Dying, Transcending }
    State state = State.Alive;

    bool collisionDisable = false;
    bool isThrusting = false;

    // Start is called before the first frame update
    void Start()
    {
        rigidBody = GetComponent<Rigidbody>();
        audioSource = GetComponent<AudioSource>();
    }
    // Update is called once per frame
    void Update()
    {
        if (state == State.Alive)
        {
            RespondToThrustInput();
            RespondToRotateInput();
        }
        if (Debug.isDebugBuild)
        {
            RespondToDebugKey();
        }

    }
    private void RespondToDebugKey()
    {
        if (Input.GetKeyDown(KeyCode.L))
        {
            LoadNextLevel();
        }
        else if (Input.GetKeyDown(KeyCode.C))
        {
            collisionDisable = !collisionDisable; //toggle
        }
    }
    void OnCollisionEnter(Collision collision)
    {
        if (state != State.Alive || collisionDisable) { return; }

        switch (collision.gameObject.tag)
        {
            case "Friendly":
                //Do nothing
                break;
            case "Finish":
                StartSucessSequence();
                break;
            default:
                StartDeathSequence();
                break;
        }
    }
    private void StartSucessSequence()
    {
        state = State.Transcending;
        audioSource.Stop();
        audioSource.PlayOneShot(sucess);
        sucessParticles.Play();
        Invoke("LoadNextLevel", levelLoadDelay);
    }
    private void StartDeathSequence()
    {
        state = State.Dying;
        audioSource.Stop();
        audioSource.PlayOneShot(death);
        deathParticles.Play();
        Invoke("LoadFirstLevel", levelLoadDelay);
    }
    private void LoadNextLevel()
    {
        int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
        int nextSceneIndex = currentSceneIndex + 1;
        if (nextSceneIndex == SceneManager.sceneCountInBuildSettings)
        {
            nextSceneIndex = 0; //loop back to start
        }
        SceneManager.LoadScene(nextSceneIndex);
    }
    private void LoadFirstLevel()
    {
        SceneManager.LoadScene(0);
    }

    private void RespondToThrustInput()
    {

        if (Input.GetKey(KeyCode.Space))
            isThrusting = Input.GetKey(KeyCode.Space);

        if (isThrusting)
        {
            ApplyThrust();
            Debug.Log("Thrust= " + mainThrust); //añadido nuevo para solucion problema
        }
        else
        {
            audioSource.Stop();
            mainEngineParticles.Stop();
        }
    }

    private void ApplyThrust()
    {
        rigidBody.AddRelativeForce(Vector3.up * mainThrust * Time.deltaTime);

        if (!audioSource.isPlaying)
        {
            audioSource.PlayOneShot(mainEngine);
        }

        mainEngineParticles.Play();
    }

    private void FixedUpdate()
    {
        if (isThrusting)
        {
            rigidBody.AddRelativeForce(Vector3.up * mainThrust, ForceMode.Acceleration);
        }
    }

    private void RespondToRotateInput()
    {
        rigidBody.freezeRotation = true; // take manual control 
        float rotationThisFrame = rcsThrust * Time.deltaTime;
        if (Input.GetKey(KeyCode.A))
        {
            transform.Rotate(Vector3.forward * rotationThisFrame);
        }
        else if (Input.GetKey(KeyCode.D))
        {
            transform.Rotate(-Vector3.forward * rotationThisFrame);
        }
        rigidBody.freezeRotation = false; //resume physics
    }

}

Hi Nina,

Thanks.

Above is the script and here’s a screenshot of the error message I get.

Thanks for your help.

I think you’re right, it must be the same bug. For example, when I increase the Drag value, the Rocket does move slower whether the Rocket or something else is selected in the Hierarchy. The difference is though that the Rocket moves MUCH slower when it is selected (as if the settings values were different from when it is not selected).

Your last screenshot says there was an issue with the Rocket script. Does the file name match the class name inside the file? For example, is the rocket class inside the rocket.cs file? Unity is case-sensitive.

Remove this line from ApplyThrust:

rigidBody.AddRelativeForce(Vector3.up * mainThrust * Time.deltaTime);

This is wrong because isThrusting will not get set to false if the space key is not being pressed:

if (Input.GetKey(KeyCode.Space))
            isThrusting = Input.GetKey(KeyCode.Space);

This, without the if, is correct:

isThrusting = Input.GetKey(KeyCode.Space);

Once the flaws and the script are fixed, test your game again. The Inspector mustn’t display that warning.

I changed Update into FixedUpdate, for some reason it solved my problem.

If you guys can also confirm this just for the feedback it will be great.

thanks

Hi Andalina,

I had the same issue, I applied your suggestion and it worked fine. I just needed to multiply my “Main Thrust” value by 10 to make my ship work similar. Otherwise ship couldn’t launch like it’s super heavy.

Still I’ll revert it back to “Update()” because in the instructor hangout for some reason, Ben and Rick didn’t recommend to use FixedUpdate() for now .

1 Like

Hi Umit, I am glad it worked.

I agree, I watched the Instructor Hangout afterwards… they mention about this “thrust issue” and that they’ll fix it by the end of the section. I’ll keep my FixedUpdate temporary until they fix the issue by the end of the section, because if I don’t it is very hard for me to test the game each time I hit play.

Hi Nina,

Thanks. I had replaced my old script with the one you suggested (did a copy-paste) and that’s when I received the error message. When I removed it and pasted back my old script the error message disappeared.
However, changing Update to Fixed Update, as Andalina suggested did the trick:)
Thanks!

Hi Andalina,

It did work, thanks :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms