I’m working on getting a little spaceship flying around but can’t get the movement just how I want it.
My requirements:
- Pressing ‘up’ accelerates the ship (caps at max speed)
- Ship maintains speed (only way to slow down is turning around and accelerating the other direction)
- Pressing ‘left’ or ‘right’ rotates ship without changing velocity
After doing plenty of googling and trial & error, I came across this code which checked most of the boxes except for one.
public class TransformFunctions : MonoBehaviour
{
public float turnSpeed = 50f;
public float _Velocity = 0.0f; // Current Travelling Velocity
public float _MaxVelocity = 1.0f; // Maxima Velocity
public float _Acc = 0.0f; // Current Acceleration
public float _AccSpeed = 0.1f; // Amount to increase Acceleration with.
public float _MaxAcc = 1.0f; // Max Acceleration
public float _MinAcc = -1.0f; // Min Acceleration
void Update()
{
if (Input.GetKey(KeyCode.UpArrow))
_Acc += _AccSpeed;
if (Input.GetKey(KeyCode.DownArrow))
_Acc -= _AccSpeed;
if (Input.GetKey(KeyCode.LeftArrow))
transform.Rotate(Vector3.up, -turnSpeed * Time.deltaTime);
if (Input.GetKey(KeyCode.RightArrow))
transform.Rotate(Vector3.up, turnSpeed * Time.deltaTime);
if (_Acc > _MaxAcc)
_Acc = _MaxAcc;
else if (_Acc < _MinAcc)
_Acc = _MinAcc;
_Velocity += _Acc;
if (_Velocity > _MaxVelocity)
_Velocity = _MaxVelocity;
else if (_Velocity < -_MaxVelocity)
_Velocity = -_MaxVelocity;
transform.Translate(Vector3.forward * _Velocity * Time.deltaTime);
}
}
This code unfortunately doesn’t allow for the ship to turn while maintaining it’s velocity. I tried messing around with local and world space but adding Space.World to transform.Translate(Vector3.forward * _Velocity * Time.deltaTime); fixes the turning issue while of course breaking the movement as seen in the second clip below.

At this point, my only guess would be that I need to maybe stop using Vector3.forward in my transform.Translate and instead, calculate that Vector3, but I’m still pretty new to this and figured I would ask to see if anyone had any thoughts before diving right back in to google.
Thanks for reading!