Time.deltatime mistake that I missed

Hi, so I think I must’ve missed something a while back in regard to how I have my Time.deltatime setup for the ApplyThrust methods that is used to propel the rocket. I was hoping to get a full snapshot of the finalized code that propels the rocket up, because right now I have my thrust variable set to 100,000 to make it work well, so something is definitely up. It took me a while to notice my mistake, since the program still works, but specifically my “ApplyThrust” and “RespondToThrustInput” seem to be slightly off, and I haven’t figured out what simple thing I missed.

Really hoping to get help with this before moving onto the next project, as I’m nearing the end of this project and want to have a solid understanding of this one’s function.

Any and all help is appreciated! Code is below, and I specifically think I messed up my methods mentioned above.

public class Rocket : MonoBehaviour
{
    Rigidbody rigidBody;
    AudioSource audioSource;
    [SerializeField] float rcsThrust = 200f;
    [SerializeField] float mainThrust = 3000f;

    [SerializeField] AudioClip mainEngine;
    [SerializeField] AudioClip successSound;
    [SerializeField] AudioClip deathSound;

    [SerializeField] ParticleSystem mainEngineParticles;
    [SerializeField] ParticleSystem successSoundParticles;
    [SerializeField] ParticleSystem deathSoundParticles;

    enum State {alive, dying, transcending}
    State state = State.alive;
        
        // 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();
            RespondToRotationInput();
        }
    }

    void RespondToThrustInput()
    {
        float thrustThisFrame = mainThrust * Time.deltaTime;
        if (Input.GetKey(KeyCode.Space)) //are you hitting space bar? Regardless, are you hitting D or A?
        {
            ApplyThrust(thrustThisFrame);
        }
        else
        {
            audioSource.Stop();
            mainEngineParticles.Stop();
        }
    }

    private void ApplyThrust(float thrustThisFrame)
    {
        rigidBody.AddRelativeForce(Vector3.up * thrustThisFrame * Time.deltaTime);
        if (!audioSource.isPlaying) //avoids layering 
        {
            audioSource.PlayOneShot(mainEngine);
        }
        if (!mainEngineParticles.isPlaying)
        {
            mainEngineParticles.Play();
        }
    }

    void RespondToRotationInput()
    {
        rigidBody.freezeRotation = true; // take manual control of rotation
        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 control
    }

    void OnCollisionEnter(Collision collision)
    {
        if (state != State.alive) {return;}
       
        switch (collision.gameObject.tag)
        {
            case "Friendly":
                //do nothing
                break;
            case "Finish":
                StartSuccessSequence();
                break;
            default:
                StartDeathSequence();
                break;
        }
    }

    private void StartSuccessSequence()
    {
        state = State.transcending;
        audioSource.Stop();
        audioSource.PlayOneShot(successSound);
        successSoundParticles.Play();
        Invoke("LoadNextScene", 1f);
    }

    private void StartDeathSequence()
    {
        print("hit something deadly");
        state = State.dying;
        audioSource.Stop();
        audioSource.PlayOneShot(deathSound);
        deathSoundParticles.Play();
        Invoke("RestartGame", 1f);
    }

    void RestartGame()
    {
        SceneManager.LoadScene(0);
    }

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

}```

Hi Ian95,

There is a strange bug in Unity which prevents the rocket from flying properly 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?

If it does and if you are experiencing issues with the rotation, you could apply this concept to the rotation code.


See also:

Thanks, maybe I was encountering a bug with unity as I was not able to launch the rocket. I adjusted it and it seems to be working now.

I’ve nearly completed this project and I just have a follow-up question: is there a copy of the master code for this project? I just want to cross reference what I have with what the final version of the project code is to stamp out inconsistencies in formatting and organization.

You can find the entire project including the git history on GitHub. The link can be found in the resources of almost all videos. To get to the latest project state, just click on the path.

Here is an example:

Click on 1) to get to the latest project state, and on 2) if you want to search within the project state of the current commit.

Ah, thats what I was missing. Thanks!

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

Privacy & Terms