Skip to main content

Collector

Written by: Akram Taghavi-Burris | © Copyright 2024
Status

The Collector class is crucial for facilitating interactions between the player (or any other object, like a harvester) and collectables in the game. It acts as a bridge that enables players to collect items seamlessly. When any collector game object comes into contact with a Collectable, the Collector class triggers the collectable's OnCollected method, ensuring that the appropriate actions are taken, such as updating scores or inventory.

Implmenting

The Collector component needs to be attached to any GameObject that should collect items, such as the player character. The interaction with collectables occurs through the OnTriggerEnter method, which requires the collector GameObject to have a collider set to trigger.

To ensure this, the Collector class requires a BoxCollider component, which is automatically set to trigger in the Awake() method. However, there may be instances where the collector object requires different types of colliders or even a standard collider for collision detection. In such cases, a Debug.Log message will inform the level designer about the collider settings, allowing them to add additional colliders as needed.

This flexibility enables designers to customize the collector's interaction capabilities while maintaining the core functionality of the component.

Collector.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(BoxCollider))]
public class Collector : MonoBehaviour
{
// Called when game object initializes
public void Awake()
{
// Get the BoxCollider component
BoxCollider boxCollider = GetComponent<BoxCollider>();

// If the collider is not a trigger, set it as a trigger
if (!boxCollider.isTrigger)
{
boxCollider.isTrigger = true;
Debug.LogWarning("Collider on collector object was not set to a trigger. It has now been set to a trigger");
}
}//end Awake()

// Triggers when an object enters the collider
private void OnTriggerEnter(Collider other)
{
// Check if the other object has a Collectable component
if (other.TryGetComponent<Collectable>(out Collectable collectable))
{
Collect(collectable);
}
}//end OnTriggerEnter

// Collect the collectable
protected virtual void Collect(Collectable collectable)
{
// Process the data for the collectable
ProcessCollectableData(collectable.CollectableData);

// Call the OnCollected method when the collectable is collected
collectable.OnCollected();
}//end Collect()

// Process the data for the collectable object
protected virtual void ProcessCollectableData(CollectableData collectableData)
{
Debug.Log($"Collected: {collectableData.CollectableName}, Points: {collectableData.PointValue}");
}//end ProcessCollectableData()
}

Key Features

  • Trigger Collider: The Collector class requires a BoxCollider to detect collisions. In the Awake() method, it checks if the collider is set as a trigger; if not, it sets the collider as a trigger automatically. This ensures that the collector can interact with collectables without obstructing movement.

  • OnTriggerEnter Method: This method is called when another collider enters the trigger zone. It checks whether the colliding object has a Collectable component using TryGetComponent. If it does, the collector proceeds to call the Collect() method.

  • Collect Method: The Collect() method is responsible for processing the collected data and calling the collectable's OnCollected() method. This ensures that any actions associated with collecting the item, such as playing sound effects or updating scores, are executed.

  • Data Processing: The ProcessCollectableData() method outputs a message to the console when a collectable is collected, providing information about the item, such as its name and point value. This method can be overridden in derived classes to implement more complex behaviors, such as updating a player's inventory or score. Since we will most likely have a score manager to handle point accumulation, and as one is not implemented in this project, we are currently using Debug.Log to output the points of the collectable to the console. This allows us to see the collected points during testing and development, paving the way for more sophisticated score management in future iterations of the project.

As we continue to develop our game, it's important to consider how we might want to keep a record of our collectables. Maintaining a collection allows us to track which items have been gathered, manage inventory, and implement features such as achievements or rewards. In the next section, we will explore how to build a system for managing these collectables in a collection, providing players with a more interactive and rewarding experience as they progress through the game.