Rigidbody.AddRelativeForce not working in build

I am following the Project Boost tutorial and tried to build what I had so far.
However, I found that my rocket was not going up when pressing space.
Below is the rocket script applied to the rocket prefab.

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Rocket : MonoBehaviour
{
    [SerializeField] float ThrustSpeed = 2;
    [SerializeField] float RCSSpeed = 1.5f;

    Rigidbody rbody;
    AudioSource asource;
    
    // Start is called before the first frame update
    void Start()
    {
        rbody = GetComponent<Rigidbody>();
        asource = GetComponent<AudioSource>();
        
    }

    void OnCollisionEnter(Collision collision) {
        switch (collision.gameObject.tag) {
            case "Friendly":
                print("safe");
                break;
            case "Fuel":
                print("fuel");
                break;
            default:
                print("dead");
                break;
        }

    }

    // Update is called once per frame
    void Update() {
        Thrust();
        Rotate(Time.deltaTime);
    }

    void Thrust() {
        if (Input.GetKey(KeyCode.Space)) {
            //transform.Translate(Vector3.up * ThrustSpeed * Time.deltaTime * 100);
            rbody.AddRelativeForce(Vector3.up * ThrustSpeed);
            if (!asource.isPlaying) { //plays thrust sound.
                asource.Play();
            }
        }
        if (Input.GetKeyUp(KeyCode.Space)) { //stops thrust sound.
            asource.Stop();
        }
    }
    void Rotate(float frameTime) {
        frameTime *= 100 * RCSSpeed;
        if (Input.GetKey(KeyCode.A) && Input.GetKey(KeyCode.D)) {
            return;
        }
        if (Input.GetKey(KeyCode.A)) {
            rbody.angularVelocity = Vector3.zero;
            /*rbody.AddTorque(Vector3.forward);*/
            transform.Rotate(Vector3.forward * frameTime);
        }
        if (Input.GetKey(KeyCode.D)) {
            rbody.angularVelocity = Vector3.zero;
            transform.Rotate(Vector3.back * frameTime);
        }
        
    }
}

After testing I found out that rbody.AddRelativeForce(Vector3.up * ThrustSpeed); is not working.
AddRelativeTorque() from the Rigidbody class also does not work.
However, rbody.angularVelocity = Vector3.zero; appears to be working well.

The thrust sound plays when the space bar is pressed, which means the line after rbody.AddRelativeForce(Vector3.up * ThrustSpeed), asource.Play(), is called and the method works.
If I instead use transform.Translate() to move the rocket up, it would work. But for some reason AddRelativeForce() does nothing.

None of these problems exist in the editor, it’s working perfectly fine in Unity Editor. But in the Windows standalone build version AddRelativeForce() and AddRelativeTorque() doesn’t work.

What could be the reason for this, and is there a fix? Thank you!
Additional Info:
I am using Unity 2020.1.6f1 Personal
Operating System is Windows 10.

Update:
I found a fix to this issue, by adding Time.deltaTime as a component into the AddRelativeForce().
The fixed Thrust() method looks like this:

void Thrust() {
        if (Input.GetKey(KeyCode.Space)) {
            //transform.Translate(Vector3.up * ThrustSpeed * Time.deltaTime * 100);
            rbody.AddRelativeForce(Vector3.up * ThrustSpeed * 1000 * Time.deltaTime);
            if (!asource.isPlaying) { //plays thrust sound.
                asource.Play();
            }
        }
        if (Input.GetKeyUp(KeyCode.Space)) { //stops thrust sound.
            asource.Stop();
        }
    }

However, I still do not understand why is Time.deltaTime needed for AddRelativeForce to work in the build version. What is causing this difference in results between the Unity Editor and the standalone build?

Hi yuligent,

Good job on fixing the problem. To be honest, I don’t know why Time.deltaTime fixed the issue in the build. The game window in Unity is a preview, which is relatively laggy. Without the editor stuff, your build is way faster than the preview. Maybe that’s the reason why the build and the preview behave differently. Another potential reason might be Unity itself. There are always bugs.


If you want, you could test this 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);
  }
}

Maybe the rocket will behave the same in the game window and in the build.

Thank you for the explaination!

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

Privacy & Terms