How to slow or stop an object with no gravity

I have a space ship which gets it movement from addRelativeForce when the verticle axis is pressed. The thing I’d like it to do is slow or stop the ship when the user stops pressing the move button. Is there a way to apply a force in the opposite direction until a certain magnitude is achieved? Also there is no gravity in the scene.

function FixedUpdate(){
		
	var moveShip = Input.GetAxis("Vertical");
	
	var movement = new Vector3(0,moveShip + boost,0);
	
	var currSpeed = rigidbody.velocity.magnitude;
	
	rigidbody.AddRelativeForce(movement * speed * Time.deltaTime);
	
	if(Input.GetKey("space") && Input.GetAxis("Vertical")){
		
		boost = 5;
		
	}else{
		
		boost = 0;
		
	}
	
}	

Thanks

2 Answers

2

You can use rigidbody drag property to slow the spaceship . another approach is to limit velocity of spaceship if it exceeds max speed like this :

void Update()
{
    if(rigidbody.velocity.magnitude>maxspeed)
    {
        rigidbody.velocity = rigidbody.velocity.normalized*maxspeed;
    }
}

Sorry for the late response. Setting the drag works perfectly. Thanks

I seems like you’re actually looking for the drag property on the rigidBody component. Set it to more than zero and your ship will start slowing down.