Posted May 24, 2025 by mangorichgamer
#code
As the game is about picking items up and dropping them off, a few dev tasks are needed.
Optional
Pickup System
On the Players Car: The basic idea is the car has a box collider and on that has a script "AbilityPickupObjects" this script has a OnTriggerEnter and once run, will check the tag of the object that it collided with. If it's "pickup" it'll get the Component ItemPickup and run the event ItemPickup.picked Up(). If it's a dropoff tag then do something similar and run ItemDropOff.DropOff().
For the pick up item. An item you can pick up has a single script "ItemPickup". This has a ScriptableObject on it which defines properties of the pickup item:
The pickup item code will play the pickup effect, call ItemPickedUp on the GameManager, call the event OnPickedUp and then destroy the item object.
Since the pickup item is using a ScriptableObject for the details on the item, I have a few different variants of the prefab: package, present, coffee. These have different names and different icons.
The GameManager.ItemPickedUp method will Add points to the players' score, record what item the player is currently carrying, start a timer and then invoke and OnItemPickup event, which the UI will have subscribed to. This is to show a panel showing the items icon and name.
The GameManager is a Singleton and on Start() will get a reference to the player using "playersCar = GameObject.FindGameObjectWithTag("Player");" and also a reference to the object on the player that will point to the next mission objective, I've called the script PointAtTarget.
The GameManager will also start a new mission. Keep track of time taken to complete a mission and record what item the player is currently carrying. It also exposes some events for the UIManager to hook into.
The UIManager is fairly simple. On Start() it'll get the GameManager instance and hook into the events: OnScoreChanged, OnItemPickedUp, OnItemDroppedOff. For each of these events it'll update the UI appropriately.
The MissionManager is in charge of creating a mission. In this game a mission has a pickup item and a dropoff location. When you've dropped off the item a new mission is created. For this class I have an array for the pickup prefabs available, array for the drop target prefabs, available pickup location and dropoff locations. It also has a local reference to the current pickup item and the current drop target.
When a new mission is created, random numbers are generated to decide what items to create and where. An instance of the pickup prefab is created and the dropoff prefab is created in the random locations chosen. The events OnPickedUp and OnDroppedOff are hooked into so it knows when the missions is complete. It is also used to update the players Target Pointer, so the pointer can point to the pickup location and once the player has Picked it up, it'll get update it to point to the Drop off location.
The PointAtTarget script was pulled partly from GoogleAI and partly from a video.
--
public class PointAtTarget : MonoBehaviour
{
public Transform player;
public Transform target;
public GameObject arrowObject;
void Update()
{
if (player != null && target != null && arrowObject != null)
{
arrowObject.SetActive(true);
// Calculate direction from player to target
Vector3 direction = target.position - player.position;
// Rotate the arrow to face the direction
Quaternion rotation = Quaternion.LookRotation(direction, Vector3.up);
arrowObject.transform.rotation = rotation;
}
else
{
arrowObject.SetActive(false);
}
}
}
--
The UI just has one panel in the center showing what the player has picked up and some text on the right showing the current score