It’s time for me to resume working on the 2d Space Shooter. Starting today, I’ll be working on the technical specs provided by GameDevHQ in the 2D game development course.
The first requirement is to allow your spaceship to speed up with thrusters when the Left Shift is pressed down and return back to normal speed when Left shift is released.
For now, I placed the attributes I need in [SerializeField] so I can easily verify the solution when I start the game.
[SerializeField]
private bool _thrusterEnabled = false;
[SerializeField]
private float _thrusterMaxSpeed = 4f;
[SerializeField]
private float _thrusterSpeedIncrease = 0f;
The logic is to change the value of _thrusterSpeedIncrease when the Left Shift is held and return it back to zero when Left-Shift is released.
if (Input.GetKey(KeyCode.LeftShift))
{
if (_thrusterEnabled)
{
return;
}
else
{
_thrusterEnabled = true;
_thrusterSpeedIncrease = _thrusterMaxSpeed;
}
}
if (Input.GetKeyUp(KeyCode.LeftShift))
{
if (!_thrusterEnabled)
{
return;
}
else
{
_thrusterEnabled = false;
_thrusterSpeedIncrease = 0;
}
}
On my movement code, you can see that _thrusterSpeedIncrease will only have an effect when Left-Shift is held:
transform.Translate(direction * Time.deltaTime * (_speed + _speedIncrease + _thrusterSpeedIncrease));
For testing, I can see from the Unity Editor that thruster is enabled and spaceship moves faster when I hold Left-Shift.