2D Games Core Programming: Quickly Collect Pickups
Objective: When the ‘C’ key is pressed by the Player, Pickups quickly move to the player.
Before adding code to the Player.cs script, I added code to the Powerup.cs script to handle changing their movement when the C key is pressed.
In the Powerup.cs script, I added a boolean variable for keeping track of the movement state and a reference to the player.
private bool _moveTowardsPlayer = false;
private Player _player;
In the Start() method, I initialized _player to reference the user’s ship. I don’t need to null check for it because the player could be dead.
_player = GameObject.Find("Player").GetComponent<Player>();
In the Powerup.cs script’s Update() method, I added conditionals around the movement code to check _moveTowardsPlayer. If it’s false, movement is normal, but if it’s true, I used the function MoveTowards() to make the powerup move towards the player.
void Update()
{
if (!_moveTowardsPlayer) // NEW
{
// move down at a speed
transform.Translate(Vector3.down * _speed * Time.deltaTime);
}
else if ((_moveTowardsPlayer) & (_player != null)) // NEW
{
// NEW
transform.position = Vector2.MoveTowards(transform.position, _player.transform.position, _speed * 2.0f * Time.deltaTime);
}
// when leave screen, destroy object
if (transform.position.y <= -_verticalLimit)
{
Destroy(this.gameObject);
}
}
For the _moveTowardsPlayer boolean variable, I added two public methods for changing it.
public void MoveTowardsPlayerEnable()
{
_moveTowardsPlayer = true;
}
public void MoveTowardsPlayerDisable()
{
_moveTowardsPlayer = false;
}
Back in the Player.cs script, in the Update() method, I added conditionals for the C key input press and release. After the C key is pressed, I followed these instructions:
- Do a null check for the powerupContainer in the _spawnManager.
- Put all the powerups in the powerupContainer into an array for counting and direct access.
- If there are powerups in the level, enable moving towards the player.
void Update()
{
// Check charge level. Restrict to min, max values
_thrusterChargeLevel = Mathf.Clamp(_thrusterChargeLevel, 0, _thrusterChargeLevelMax);
// Set _canUseThrusters depending on _thrusterChargeLevel
if (_thrusterChargeLevel <= 0.0f)
{
_canUseThrusters = false;
}
else if (_thrusterChargeLevel >= 0.0f)
{
_canUseThrusters = true;
}
if (Input.GetKeyDown(KeyCode.LeftShift) && _canUseThrusters)
{
SpeedBoostActiveShift();
}
if (Input.GetKeyUp(KeyCode.LeftShift))
{
SpeedReset();
}
// NEW
// If "c" key is pressed, call method to make powerups move towards player
if (Input.GetKeyDown(KeyCode.C))
{
if (_spawnManager.powerupContainer != null)
{
Powerup[] _allPowerups = _spawnManager.powerupContainer.GetComponentsInChildren<Powerup>();
if (_allPowerups.Length > 0)
{
for (int i = 0; i < _allPowerups.Length; i++)
{
_allPowerups[i].MoveTowardsPlayerEnable();
}
}
}
}
// NEW
if (Input.GetKeyUp(KeyCode.C))
{
if (_spawnManager.powerupContainer != null)
{
Powerup[] _allPowerups = _spawnManager.powerupContainer.GetComponentsInChildren<Powerup>();
if (_allPowerups.Length > 0)
{
for (int i = 0; i < _allPowerups.Length; i++)
{
_allPowerups[i].MoveTowardsPlayerDisable();
}
}
}
}
CalculateMovement();
if (Input.GetKeyDown(KeyCode.Space) && Time.time > _canFire )
{
Firelaser();
}
}
When the game is tested, pressing the C key will make the powerups move quickly towards the player.