- Home /
Rotating gameobject while maintaining velocity
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!
Answer by FlaSh-G · Aug 10, 2021 at 02:54 PM
The script is moving the ship in the direction it is facing (Vector3.forward, with Transform.Translate using local space by default, so "my forward").
If you don't want that, you need to store the movement direction of the object in your script and apply that movement direction regardless of direction.
public class SpaceshipMovement : MonoBehaviour
{
private Vector3 velocity;
public float acceleration = 20f;
public float turnSpeed = 50f;
private void FixedUpdate()
{
if (Input.GetKey(KeyCode.UpArrow))
{
velocity += transform.forward * acceleration * Time.deltaTime;
}
if (Input.GetKey(KeyCode.DownArrow))
{
velocity -= transform.forward * acceleration * Time.deltaTime;
}
if (Input.GetKey(KeyCode.LeftArrow))
{
transform.Rotate(Vector3.up, -turnSpeed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.RightArrow))
{
transform.Rotate(Vector3.up, turnSpeed * Time.deltaTime);
}
transform.Translate(velocity * Time.deltaTime, Space.World);
}
}
This would be the code for sticking with you current approach, but I'd like to point out that what you want looks very much like a good use case for a Rigidbody2D component, which would take care of velocity and acceleration for you.
Thanks for the quick reply! That was exactly what I was looking for. Now time to read up on Rigidbody2D