making my ship stop after movement

Hi guys,

I currently have a spaceship moving back and forward left and right and up and down. I am trying to make my character stop with the S key but it keeps moving I have tried to apply it but it seems not to be working please see my code anything I may be doing wrong. I have defined a variable called stopShip to 0 so when the S key is pressed there will be no value added to it so should come to a standstill any help would be great: :slight_smile:

public class MoveCar : MonoBehaviour 
{
	
    public float moveSpeed = 2f;
    public float turnSpeed = 2f;
    public float stopShip = 0;   // defined it 0 so there is no speed


   void FixedUpdate()
		{
			if(Input.GetKey (KeyCode.F)) // forwards
			{
			rigidbody.AddForce(-moveSpeed,0,0);
			}
		else
			if(Input.GetKey (KeyCode.S))  // make the character stop 
			{
			rigidbody.AddForce(-stopShip,0,0);
			}

Yes it will continue to move with your current script. “ADDING” 0 force WILL NOT MAKE an object come to rest. It’s like adding a 0 force on an object which is already moving. 0 force = no force. You have to set the force, not add it, when you want to stop. Instead set the velocity to 0

rigidbody.velocity = Vector3.zero;

okay I understand now thanks for clearing this up its much appreciated