Project Boost - rocket will not thrust when exported to either WebGL or Windows

Hello everyone,

I have a very strange issue where the rocket thrust will not work after I have exported my finished game to either WebGL or a Windows executable.

I believe it is connected to another issue I encountered yesterday which I resolved at the time. The issue I encountered was the same as this, and I referred to Nina’s Github pull request fix to get the solution. Here’s the link to the first issue for reference:
Time.deltaTime problems

However, in Nina’s solution in the above link, she created a FixedUpdate method and placed the new ForceMode.Acceleration (which replaced Time.deltaTime) in it. When I did the same, plus created a isThrusting boolean etc, it didn’t work for me. The rocket still didn’t thrust.

However, I found in my code that if I simply replaced Time.deltaTime with ForceMode.Acceleration in the original ApplyThrust method - which is called in the original Update method (as opposed to FixedUpdate), it worked for me.

So all was good until today when I finished the game and tried exporting it.

Boom! Now the rocket isn’t thrusting again in the exported versions, exactly the same way it wasn’t yesterday before my initial fix. It’s working fine in the editor.

I have attempted to use Nina’s original solution for the first issue again (using FixedUpdate etc.) as I thought that might be what’s causing it to not work when the project is exported. “Perhaps WebGL or an .exe require FixedUpdate but the editor doesn’t require it?” But no, it didn’t solve it (not working in the editor again)

I have included the code file for reference (it uses the Update method currently)

Can anybody please help me?

using UnityEngine;
using UnityEngine.SceneManagement;

public class Rocket : MonoBehaviour
{
    [SerializeField] float rcsThrust = 300f; // rotation speed
    [SerializeField] float mainThrust = 30000000f; // thrust amount
    [SerializeField] float levelLoadDelay = 2f;

    [SerializeField] AudioClip mainEngine;
    [SerializeField] AudioClip success;
    [SerializeField] AudioClip death;

    [SerializeField] ParticleSystem mainEngineParticles;
    [SerializeField] ParticleSystem successParticles;
    [SerializeField] ParticleSystem deathParticles;

    Rigidbody rigidBody;
    AudioSource audioSource;

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

    bool collisionsDisabled = false;

    // Start is called before the first frame update
    void Start()
    {
        rigidBody = GetComponent<Rigidbody>(); // Gets the rocket's rigidbody component
        audioSource = GetComponent<AudioSource>(); // Gets the rocket's audio source component
    }

    // Update is called once per frame
    void Update()
    {
        if (state == State.Alive)
        {
            RespondToThrustInput();
            RespontToRotateInput();
        }
        if (Debug.isDebugBuild)
        {
            RespondToDebugKeys();
        }
    }

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

    private void RespondToThrustInput()
    {
        if (Input.GetKey(KeyCode.Space))
        {
            ApplyThrust();
        }
        else
        {
            StopApplyingThrust();
        }
    }

    private void StopApplyingThrust()
    {
        audioSource.Stop(); // Stops audio when space not down
        mainEngineParticles.Stop();
    }

    private void ApplyThrust()
    {
        // ForceMode.Acceleration used because using deltatime made the rocket not move. Strange issue.
        rigidBody.AddRelativeForce(Vector3.up * mainThrust, ForceMode.Acceleration);
        if (!audioSource.isPlaying) // Prevents audio from layering
        {
            audioSource.PlayOneShot(mainEngine);
        }
        mainEngineParticles.Play();
    }


    private void RespontToRotateInput()
    {
        rigidBody.angularVelocity = Vector3.zero; // remove rotation due to physics

        float rotationThisFrame = rcsThrust * Time.deltaTime;

        if (Input.GetKey(KeyCode.LeftArrow) && !Input.GetKey(KeyCode.RightArrow))
        {
            transform.Rotate(Vector3.forward * rotationThisFrame);
        }

        else if (Input.GetKey(KeyCode.RightArrow) && !Input.GetKey(KeyCode.LeftArrow))
        {
            transform.Rotate(-Vector3.forward * rotationThisFrame);
        }
    }

    void OnCollisionEnter(Collision collision)
    {
        if (state != State.Alive || collisionsDisabled)
            return; // ignore collisions when dead or when in debug mode

        switch (collision.gameObject.tag)
        {
            case "Friendly":
                break;
            case "Finish":
                StartSuccessSequence();
                break;
            default:
                StartDeathSequence();
                break;
        }
    }

    private void StartSuccessSequence()
    {
        state = State.Transcending;
        audioSource.Stop(); // stops main engine sound
        audioSource.PlayOneShot(success);
        successParticles.Play();
        Invoke("LoadNextScene", levelLoadDelay); // loads next level after levelLoadDelay seconds
    }

    private void StartDeathSequence()
    {
        state = State.Dying;
        audioSource.Stop(); // stops main engine sound
        audioSource.PlayOneShot(death);
        deathParticles.Play();
        Invoke("LoadCurrentLevel", levelLoadDelay);
    }

    private void LoadCurrentLevel()
    {
        int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
        SceneManager.LoadScene(currentSceneIndex);
    }

    private void LoadNextScene()
    {
        int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
        int nextSceneIndex = currentSceneIndex + 1;
        if (nextSceneIndex == SceneManager.sceneCountInBuildSettings)
        {
            nextSceneIndex = 0; // loop back to first level
        }
        SceneManager.LoadScene(nextSceneIndex);
    }
}

Hi Ivan,

I skimmed your code and didn’t spot any issue. Does the rotation work? If not, check if the state is set to State.Alive.

Please follow Rob’s instruction in this thread. Maybe there is a helpful error message in your browser console.

I took your file and used it in place of the stock (from github) Rocket script.

1. I am able to export to the webgl without issue.

2. I am seeing the rocket thrust or move and assuming there is nothing else broken is some other file the only problem I make out is it appears the thrust logic has been modified and you are using such a high value for the “main thrust” that the rocket is basically flying out of view at high velocity at the first touch of the space bar. You dont see the rocket in the screen shot because it has already reached mars by now :grin:. Also I saw this over-speed issue when running in both editor and webgl.

3. So if you look the properties window screen shot below you will see that the stock script uses a thrust value of around 2000 while yours has it at 30 million ! I am assuming you increased this in testing but in any case this is way too high. Infact even 2000 was making it go up to too fast hence my previous assumption regarding the thrust logic having being changed in your version.
To make it match the stock thrust somewhat I had to set the main thrust at just 100 when using your code.

4. So in conclusion based on this file alone I am not able to see a issue in exporting to webgl and the only issue was with the “hyper thrust” behaviour which was fixed by lowering the “main thrust” value to around 100.
You could try getting the stock code from github try building that out in webgl and if it does it should mean there is nothing wrong with your unity setup. You could then compare the 2 versions and may get to the issue faster.

I tested using the version below…

image

Hi Nina, Codermonk,

I changed the variable in the code from 30 million to 3 and the rocket feels the exact same when I play it in Unity. Also, the Main Thrust variable as viewed from the inspector was set to 3 even when the variable was still at 30 million in code.

Any change I make to the variable value in code does not seem to change the variable value when viewed in the inspector, even when restarting Unity afterwards.

I have to delete the rocket script component and re-add the script to the rocket prefab to get it to update in the inspector.

Anyway, I changed the mainThrust value to 5 and felt that was a suitable speed. There is, however, a new issue now when I export to WebGL:
The rocket is now thrusting in the exported version (as opposed to not thrusting at all) but is significantly slower than when played in Unity. If the mainThrust setting is 5 when played in Unity, when the exported version is played it feels like its set to 1.

Hi Grand

Is this still and issue or has it been solved.
Checking in for clarity

@Nina Left this one live just in case

Maybe you could test this code in 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?

Hi everyone,

Thanks for your replies. I have since stopped doing the course to pursue other ventures but I can confirm that I was still experiencing the issue when I stopped.

Hopefully someone else on the course who is experiencing the same issue might get some help from this thread.

Thanks for letting us know, Ivan. :slight_smile:

This topic was automatically closed after 4 hours. New replies are no longer allowed.

Privacy & Terms