Spaceship Movement and Momentum

I’m a novice on the math calls to make this spaceship to move how I would like it to.

  • Spaceship has a rear engine. This increases the velocity in a facing direction.
  • Spaceship has rotation thrusters which do not change the forward momentum.
  • Space ship has a weaker front engine that decreases the velocity in the facing direction.
  • When the space ship turns and fires its rear engines, the vector should change as part of a calculation. So the fastest stop for example would be a 180 degree flip and blast engines. A 90 degree turn will only change the momentum vector a percentage based on physic calc.

Currently I have the force for moving forward working but if I turn I just change direction and my speed continues on the new vector. I tried mucking with rigidbody.AddRelativeForce (Vector3.forward*0.1); but I couldn’t get it working. Any tips on this?

void Update () {
		transform.Translate(Vector3.up * Time.deltaTime * speed);
		if(Input.GetKey(KeyCode.W)) {
			speed += 0.01f;
			if (speed > 2.5f)
				speed = 2.5f;
		} 
		if(Input.GetKey(KeyCode.S)){
			speed -= 0.003f;
			if (speed < 0.0f)
				speed = 0.0f;		
		} 

	
		if(Input.GetKey(KeyCode.A)) transform.Rotate(0,0,90 * Time.deltaTime); 
		if(Input.GetKey(KeyCode.D)) transform.Rotate(0,0,-90 * Time.deltaTime); 

		Vector3 position = transform.position;
	}

Anyway, I figured it out. I appreciate the reply Robertbu. It gave me a bit of the direction. So many people on the forums but so few answers. I miss the flashpunk community. :frowning:

The concept that was hard for me as a unity novice was the transform. Transform.up returns the current direction (assuming your model is facing up). This is different from position and rotation, giving a value between 1 and -1. You can then multiple the force applied to it.

Another useful thing is you can control the maximum of both the velocity and rotation speed (torque). The default max speed for both is 7.

Another note is once you have rigidbody added to your gameobject you don’t need to add the constant force component as rigidbody seems to incorporate all of it.

		if (Input.GetKey(KeyCode.W)){
			rigidbody.AddForce(transform.up * 0.4f);
		}
		
		if (Input.GetKey(KeyCode.S)){
			rigidbody.AddForce(transform.up * -0.2f);
		}
		

		if(Input.GetKey(KeyCode.A)) rigidbody.AddTorque(0,0,0.4f);
		if(Input.GetKey(KeyCode.D)) rigidbody.AddTorque(0,0,-0.4f); 

Hopefully this helps someone.