Project Boost, not thursting

Hello guys, im not finding a solution to my rocket not thrusting, this is the code im using:

using System;
using UnityEngine; //namespaces
using UnityEngine.SceneManagement;

public class Rocket : MonoBehaviour
{
    Rigidbody rb;
    AudioSource audioSource;
    public float levelLoadDelay;
    //enum State { Alive, Dying, Transcending }  replaced by istransitioning
    //State state = State.Alive;  replaced by istransitioning

    bool isTransitioning = false;



    [SerializeField] float rcsThrust = 100f;
    [SerializeField] float mainThrust = 100f;

    [SerializeField] AudioClip mainEngine;
    [SerializeField] AudioClip success;
    [SerializeField] AudioClip dead;

    [SerializeField] ParticleSystem mainEngineParticles;
    [SerializeField] ParticleSystem successParticles;
    [SerializeField] ParticleSystem deadParticles;

    bool collisionsDisabled = false;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
        audioSource = GetComponent<AudioSource>();

    }



    void Update()
    {
        ProcessInput();
    }

    private void ProcessInput()
    { if (!isTransitioning)
        {
            RespondToThrustInput();
            Rotate();
        }
        if (Debug.isDebugBuild)
        {
            RespondToDebugKeys();
        }

    }

    private void RespondToDebugKeys()
    {
       if (Input.GetKeyDown(KeyCode.L))
        {
            LoadNextScene();
        }
       else if (Input.GetKeyDown(KeyCode.C))
        {
            collisionsDisabled = !collisionsDisabled;
        }    
    }

    private void OnCollisionEnter(Collision collision)  // Type Collision . Variable collision 
    {
        if (isTransitioning || collisionsDisabled ) { return; }
     

        switch (collision.gameObject.tag)
        {     
            case "Friendly":
                print("Collide with Friendly");
            break;
            case "Respawn":
                print("Collide with Respawn");
            break;
            case "Finish":
                StartSuccessSequence();
                break;
            default:
                StartDeathSecuence();
                break;
        }

    
    }


    private void StartSuccessSequence()
    {
        isTransitioning = true;
        audioSource.Stop();
        audioSource.PlayOneShot(success);
        successParticles.Play();
        Invoke("LoadNextScene", levelLoadDelay);
    }
    private void StartDeathSecuence()
    {
        isTransitioning = true;
        audioSource.Stop();
        audioSource.PlayOneShot(dead);
     
        deadParticles.Play();
        Invoke("RestartLevel", levelLoadDelay);
    }
    private void LoadNextScene()
    {
        int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
        int nextSceneIndex = currentSceneIndex + 1;
        if (nextSceneIndex == SceneManager.sceneCountInBuildSettings)
        {
            nextSceneIndex = 0; //LoopBackToStart
        }
        SceneManager.LoadScene(nextSceneIndex);
  
    }
    private void RestartLevel()
    {
        SceneManager.LoadScene(0);
    }
    private void RespondToThrustInput()
    {
        
        if (Input.GetKey(KeyCode.Space)) //can thrust while rotating
        {
            ApplyThrust();
        }
        else
        {
            StopApplyingThrust();
        }
    }


    private void ApplyThrust()
    {

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

        if (audioSource.isPlaying == false)
        {
            audioSource.PlayOneShot(mainEngine);
        }
        mainEngineParticles.Play();

    }
    private void StopApplyingThrust()
    {
        audioSource.Stop();
        mainEngineParticles.Stop();
    }

    private void Rotate()
    {
        rb.angularVelocity = Vector3.zero;

     
        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);
        }


    }
}

Here the rocket components and configuration:

![image|190x500](upload://7jfl9hU186Di4bTTttqe8IxEKvX.png) 

i forgot to mention that IT DID thrust before adding time delta time but it looks inconsistent between the playing the game in unity and exporting it

Hi Zeke,

Welcome to our community! :slight_smile:

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?

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


See also:

Hi Nina,

Thanks for your help! :slight_smile:
Yeah without using Time.deltaTime the rocket will thrust, but the speed its not the same when the game is exported. I tried also using ForceMode.Acceleration but it has different speed too.
Is there anyway to have the same fps speed without using Time.deltaTime?

Here is a video of the speed dif:
https://streamable.com/gd2dsk

Did you implement the code I suggested?

Now i put the exact same coda that you provide and it works perfect :slight_smile: thanks a lot Nina!
I think that not using the FixedUpdate was my problem.

Thanks! :slight_smile:

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

Privacy & Terms