Crafting Intuitive Pickup Mechanics in Unity

Damian Dąbrowski
3 min readMar 9, 2024

--

One common feature in games is the ability for players to collect pickups, such as power-ups or health boosts, which can significantly impact gameplay dynamics. This article explores an innovative approach to this mechanic, allowing pickups to actively move towards the player upon a specific key press. Here’s a detailed breakdown of implementing this “magnetic” pickup collect feature in Unity, complete with code examples and explanations.

Concept Overview

The goal is to enhance the traditional pickup mechanic by introducing an element of control. Instead of the player needing to navigate directly over a pickup to collect it, we implement a system where pressing the ‘C’ key causes all pickups within the game scene to move towards the player’s position. This mechanic not only adds a strategic layer to gameplay but also increases the accessibility and dynamism of the pickup process.

Implementing the Pickup Script

To achieve this, modifications are made to the pickup script, allowing it to respond to player input and adjust its movement accordingly.

Code Explanation

The following is the modified PowerUp class, responsible for the behavior of the pickup objects:

public class PowerUp : MonoBehaviour
{
[SerializeField]
private float _speed = 3.0f; // Normal downward movement speed
private float _step; // Step size for moving towards the player
private float _pickupCollectSpeed = 6.0f; // Speed at which the pickup moves towards the player
private Player _player; // Reference to the player object
private void Start()
{
// Find and store a reference to the Player object in the scene
_player = GameObject.Find("Player").GetComponent<Player>();
}
void Update()
{
// Normal downward movement
transform.Translate(Vector3.down * _speed * Time.deltaTime);
// Destroy the pickup if it moves out of the game area
if (transform.position.y < -8.0f)
{
Destroy(this.gameObject);
}
// Check for the 'C' key press to initiate movement towards the player
if (Input.GetKey(KeyCode.C))
{
// Calculate the step size based on speed and frame time
_step = _pickupCollectSpeed * Time.deltaTime;
// Move the pickup towards the player's position
transform.position = Vector3.MoveTowards(transform.position, _player.transform.position, _step);
}
}
}

Key Components of the Solution

  • Player Position Reference: The script starts by finding and storing a reference to the player object. This is crucial for determining the direction in which the pickup should move.
  • Conditional Movement: Within the Update method, the script continuously checks if the 'C' key is pressed. Upon detection, the pickup's position is incrementally adjusted towards the player's position using Vector3.MoveTowards.
  • Adjustable Speed: The speed at which the pickup moves towards the player is configurable, allowing for easy adjustments to the mechanic’s feel and responsiveness.

By integrating this pickup collect mechanic, developers can offer players a more interactive and flexible way to interact with pickups, enhancing the overall game experience. This feature not only adds strategic depth but also introduces a novel way for players to engage with the game environment. Experimenting with such mechanics can lead to innovative gameplay elements that keep players engaged and excited about the possibilities within the game world.

--

--

Damian Dąbrowski
Damian Dąbrowski

Written by Damian Dąbrowski

Hi, I’m Damian, an Electrical Power Engineer, who loves building AI powered apps.

No responses yet