- Home /
RigidBody immediately stops after AddForce
I saw this question asked here: https://answers.unity.com/questions/1116019/how-do-you-fix-a-rigidbody-stopping-quickly-after.html but wasn't answered sufficiently.
I have a spaceship GameObject and all I'm doing is this:
if (Input.GetKey (KeyCode.Space)) {
rb.AddForce (transform.forward * 100, ForceMode.Impulse);
}
And as soon as I let go of the key, the ship stops moving. What I'm wanting is for the boost to decay (like it's supposed to). When I use GetKeyDown(), which is closer to what I'm going for, it just jumps forward for a split second.
The ship is supposed to be moving forward constantly, and I've tried this both by modifying the position directly and by keeping a constant velocity on the RigidBody, but I get the same result.
There is no drag on the RigidBody. I've tried it without all the other movement and RigidBody code. I've tried it in both Update() and FixedUpdate(). I've tried all the other ForceModes. The GameObject has no other scripts attached.
public class PlayerController : MonoBehaviour {
public float tilt;
public Boundary moveBoundary;
public float forwardSpeed;
public int pickups = 0;
public int missedPickups = 0;
public int enemies = 0;
public int missedEnemies = 0;
private Rigidbody rb;
private GameController gameController;
private Shooter shooter;
void Awake ()
{
GameObject gameControllerObject = GameObject.FindWithTag("GameController");
if (gameControllerObject != null)
{
gameController = gameControllerObject.GetComponent<GameController>();
}
if (gameController == null)
{
Debug.Log("Cannot find 'GameController' script");
}
rb = GetComponent<Rigidbody> ();
shooter = GetComponent<Shooter> ();
}
void Start ()
{
}
void Update ()
{
if (Input.GetButton ("Fire1") /*&& gameController.CanShoot()*/) {
shooter.Shoot ();
//gameController.AddShot ();
}
transform.position += transform.forward*forwardSpeed*Time.deltaTime;
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 targetRotation = new Vector3 (-1 * moveVertical, moveHorizontal, -1 * moveHorizontal) * 20;
transform.rotation = Quaternion.Lerp (transform.rotation, Quaternion.Euler (targetRotation), Time.fixedDeltaTime * tilt);
transform.position = new Vector3 (
Mathf.Clamp (rb.position.x, moveBoundary.xMin, moveBoundary.xMax),
Mathf.Clamp (rb.position.y, moveBoundary.yMin, moveBoundary.yMax),
rb.position.z
);
}
void FixedUpdate()
{
if (Input.GetKey (KeyCode.Space)) {
rb.AddForce (transform.forward * 50, ForceMode.Impulse);
}
//rb.velocity = transform.forward * forwardSpeed;
}
void OnTriggerEnter (Collider other) {
if (other.gameObject.CompareTag ("Pickup")) {
pickups += 1;
Destroy (other.gameObject);
}
}
}
Are you setting the rigidbody's velocity anywhere in your code?
Could you post the entirety of your script?
I was, but again, I've tried commenting out all the movement code but it doesn't fix it.
I also can't find any obvious mistakes in your script. I'd suggest to you that you rewrite that script and keep it simple to start with:
At first only add the fixed update function as it is now. Then bit by bit add the code you are currently using until you get the same problem. In case you encounter the problem from start on: Use the script in a different Scene /Testproject with default settings. Then try to trace the differences.
Answer by jobobbel · Feb 04, 2019 at 09:22 PM
@icolthar Hello, I was facing the same problem. I discovered at least an improvement: play with the values of "Gravity" in Edit > Project Settings > Input, there choose the axis you want and set Gravity between 0.05 and 0.5 . If you set the value too low, the ship will increase speed without accelerating... find a fitting value for all axis you need and voila.
I hope its working for you, too. Let me know! :-D Cheers, Jo
Your answer
Follow this Question
Related Questions
Unity Physics On Input Issue? 0 Answers
Interpolate problem? Jump isn't smooth... 1 Answer
Force to Velocity scaling? 2 Answers
Making a bubble level (not a game but work tool) 1 Answer
Sometimes the ball isn't moving 0 Answers