My rocket stops flying

using UnityEngine.SceneManagement;
using UnityEngine;


public class Rocket : MonoBehaviour
{
    [SerializeField] float rcsThrust = 100f;
    [SerializeField] float mainThrust = 30f;
    [SerializeField] AudioClip MainEngine;
    [SerializeField] AudioClip OnDeath;
    [SerializeField] AudioClip NextLevel;
    [SerializeField] float LevelLoadDelay = 1f;

    [SerializeField] ParticleSystem MainEngineParticles;
    [SerializeField] ParticleSystem OnDeathParticles;
    [SerializeField] ParticleSystem NextLevelParticls;


    Rigidbody rigidBody;
    AudioSource audiosource;

    enum State { Alive, Dying, Transcending}
    State state = State.Alive;

    


    void Start()
    {
        rigidBody = GetComponent<Rigidbody>();
        audiosource = GetComponent<AudioSource>();
    }

    
    void Update()
    {
        
        if (state == State.Alive)
        {
            RespondToThrust();
            RespondToRotation();
        }
        

    }

    private void OnCollisionEnter(Collision collision)
    {
        if (state != State.Alive) { return; } //Stop the execution


        switch (collision.gameObject.tag)
        {
            case "Friendly":
                break;

            case "Finish":
                StartSuccessSequence();

                break;
            default:
                StartDeathSequence();

                break;



        }
        
    }

    private void StartSuccessSequence()
    {
        state = State.Transcending;
        audiosource.Stop();
        audiosource.PlayOneShot(NextLevel);
        NextLevelParticls.Play();
        Invoke("LoadNextScene", LevelLoadDelay);
        print("Pass");
    }

    private void StartDeathSequence()
    {
        state = State.Dying;
        audiosource.Stop();         //To stop the current sound
        audiosource.PlayOneShot(OnDeath);
        OnDeathParticles.Play();
        Invoke("LoadFirstLevel", LevelLoadDelay);
    }

    

    private void LoadFirstLevel()
    {
        SceneManager.LoadScene(0);
    }

    private void LoadNextScene()
    {
        SceneManager.LoadScene(1);//todo allow for more than 2 levels.
    }





    private void RespondToRotation()
    {
        ApplyRotation();
    }

    private void ApplyRotation()
    {
        float rotationThisFrame = rcsThrust * Time.deltaTime;

        rigidBody.freezeRotation = true;
        if (Input.GetKey(KeyCode.A))
        {
            print("Left");
            transform.Rotate(Vector3.forward * rotationThisFrame);
        }
        else if (Input.GetKey(KeyCode.D))
        {
            print("Right");
            transform.Rotate(-Vector3.forward * rotationThisFrame);
        }
        rigidBody.freezeRotation = false;
    }
     



    private void RespondToThrust()
    {
        if (Input.GetKey(KeyCode.Space))
        {
            ApplyThrust();

        }
        else
        {
            audiosource.Stop();
            MainEngineParticles.Stop();
        }

    }

    private void ApplyThrust()
    {
        float ThrustThisFrame = mainThrust * Time.deltaTime;



            rigidBody.AddRelativeForce(Vector3.up * ThrustThisFrame );

            if (!audiosource.isPlaying)
            {
                audiosource.PlayOneShot(MainEngine);
            }

            MainEngineParticles.Play();

       
        
    }
}

after multiplying Time.deltaTime in my Thrust , my rocket stops flying :cry: :cry: Why?
though my thrust partical system is working fine

Hi sasi,

Have you already compared your code to the Lecture Project Changes which can be found in the Resources of this lecture?


See also:

yup I checked it.
nothing seems different.

Have you already tried to increase the mainThrust value?


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 that didn’t work, try to remove Time.deltaTime from the AddRelativeForce method. Alternatively, test this potential 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);
    }
}

Did this fix the issue?

No, I had confirmed it . It isn’t related to bug and removing Time.deltaTime is a fix but my object becomes frame rate dependent :cry:

Did you test my alternative solution?

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

Privacy & Terms