I’m currently creating a game similiar to Lunar Lander, but i’m currently stuck on a weird problem. Rigidbody.AddForce completely works in the Editor, but not in the final build/during runtime. Here’s my affected script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Controller : MonoBehaviour
{
public GameObject rocket;
public ParticleSystem particles;
public Rigidbody2D rbRocket;
public float thrust = 1.0f;
public float mass;
public static float fuel = 100;
public float loss = 1;
public static bool SAon = false;
public bool wastingFuel = false;
// Start is called before the first frame update
void Start()
{
particles = GetComponent<ParticleSystem>();
rbRocket = rocket.GetComponent<Rigidbody2D>();
particles.Stop();
}
// Update is called once per frame
void Update()
{
if (fuel <= 0)
{
return;
}
if (Input.GetKey(KeyCode.W))
{
rbRocket.AddForce(Time.timeScale * thrust * transform.up);
}
float horizontalKeys = Input.GetAxis("Horizontal");
rbRocket.AddTorque(-horizontalKeys * (rbRocket.mass / 4));
if (Input.GetKey(KeyCode.W))
{
fuel -= Time.deltaTime * loss;
particles.Play();
}
if (Input.GetKeyUp(KeyCode.W))
{
particles.Stop();
}
if (SAon)
{
rbRocket.angularDrag = 5;
}
else
{
rbRocket.angularDrag = 1;
}
if (Pause.isPaused == true)
{
}
}
void FuelGone()
{
}
}
(The player flies using AddForce in the editor, but doesn’t in the final build)
NOTE: I’m not able to use FixedUpdate() since that makes that problem also occur in the editor.