Pickup and drop off - Day 4


As the game is about picking items up and dropping them off, a few dev tasks are needed.

  • System to create pickup objects and drop off targets
  • Ability for the player to pick and object up
  • Ability to get points or money when the drop off happens - amount might be dependent on time taken
  • System to show where the drop off is. To begin with this will be a simple arrow pointing the way, but if dev time permits, I'd like a dotted path to show, from the player to the target drop off point

Optional

  • When you pick up an item (this maybe a package or a person) then it'd be nice if it showed a message / dialog, saying where the person is sending the item or the passenger, perhaps even have a voice . 
  • Would be good to show a picture of the item being picked up. AI generated portrait perhaps? or just an icon.
  • If it is a timed objective, once the item is picked up, it should show a timer counting down. Right now, I have no idea how the time required will be calculated and how that will translate to points given.
  • Show the money the player has, that way when they get the reward, they can see number going up!

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:

  • pickup effect - the particle effect to create when the item is picked up
  • drop off effect - the effect to create when the item is dropped off
  • score to add on pickup
  • score to add on drop off
  • icon - icon of the item e.g. parcel, coffee, present
  • item name

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

Leave a comment

Log in with itch.io to leave a comment.