Do I need a Coroutine to handle acceleration?
I started working on what I thought would be very simple space shooter over 6 months ago and while I have come very far, one thing has eluded me and that has been the ship controls. I'm not certain what I am doing wrong and I have no hair left to tear out. I've tried dozens of tutorials and cant quite get what I want. I'm creating a top-down space shooter in 3D and despite the numerous tutorials, I cant find any that cover what i need. I want to make a ship that accelerates towards a top speed when the up key is pressed and I want the ship to continue its currents trajectory if the key is released maintaining its current speed even if it has not met the top speed. I would like the down key to pull back on the ship like brakes/reverse until a maximum deceleration is met and once again maintaining a current speed if the key is released. I don't know what I need for this, maybe a Coroutine?
{
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);
}
}`
Your answer
