Enable Resource Drop Off
In this tutorial, you will learn how to enable units to drop off resources at a drop-off target point (building) for them to be actually collected and added to the faction's resources.
1. Configure Resource Prefab
Open the tutorial_resource prefab and navigate to its Resource component on the Resource tab and disable the Auto Collect field to make it a requirement to have drop off points to get this resource added to the collector's faction.
2. Configure Drop Off Target (Building)
In the same scene, select the local player faction's tutorial_building instance.
Under its building_components child object, add the Drop Off Target component.
Configure the newly added component like the following:
- Code: Unique code to distinguish this entity component from other entity components attached to the same building entity.
- Allowed Resource Types: Define what resource types collectors are allowed to drop off here. For this example, we will be setting this to All meaning that we will accept all resource types.
- Require Drop Off Position: Enable this field in order to force collectors to drop off their resources at a fixed position in relation to the faction entity. When disabeld, collectors will be allowed to drop off their resources as soon as they enter in range of the faction entity radius.
- Drop Off Position: When Require Drop Off Position is enabled, this is the position where the resource collector unit would have to move to in order to drop off its collected resources correctly. You can use the already available unit_spawn_position child object or create a new one like the resource_dropoff_position used here:
- Forced Terrain Areas: You can either leave this array field empty, to allow all terrain types to include the drop off target position or add the only used Terrain Area Type which would be the ground_terrain one.
3. Configure Drop Off Source (Collector Unit)
Open the tutorial_unit prefab and add the Drop Off Source to its unit_components child object:
In the beginning of this component's inspector, you can see:
Entity Component (Source: IUnit - Target: IFactionEntity with IResourceDropOff)
This means that this component is attachable to entities of type IUnit (which refers to the interface used by unit entities) and that it can have IFactionEntity (interface used on unit and building entities) with a component that implements the IResourceDropOff interface such as the Drop Off Target as its target. Let's go ahead and start configuring it.
3.1 General Tab
- Code: Use a unique code among the other entity components attached to the same entity.
- Is Active: Make sure this is enabled.
- Priority: Since there are three other components that can have an active target that have the 0, 1 and 2 as values in the Priority field, then we will make this component have a priority of 3, marking it as the least prioritized component when the local player has the unit selected and right-clicks on a target entity or position.
3.2 Target Search/Picker
This component actively monitors the faction's drop off points and always picks the closest one to the resource the unit is actively collecting.
- Target Picker: The drop off target that we will be testing with is the tutorial_building so no need to perform filtering on the type of the entity that has a drop off target component. Therefore we can leave this assigned to All.
3.3 Setting Target
- Set Target Task UI: An entity component with an ability to set a target allows the player to set that target either by a right-mouse click on the target while the entity is selected or by a dedicated task that appears in the dedicated UI task panel when the entity is selected as well. The data that defines the task are assigned here. We went through setting up such a task in the Add Movement Task section. However we will leave this component without a dedicated target assignment task.
- Order Audio: This field allows to assign one or multiple audio clips where one is played when the player assigns a new target. If you have multiple audio clips to assign, then the Fetch Type field allows you to determine how the clip is chosen each time. The Cooldown field allows you to input a cooldown time for playing the audio clip. In case you do not want the target assignment order audio to be played each time when you are spamming it, then a cooldown value of > 0 would allow for that.
3.4 Dropoff Resources/Capacity
For each Resource Type that the unit can collect and drop off, you need to define an element for it in the Drop Off Resources array field. Since we only have the tutorial_resource_wood type until now, let's add one element to the array in the inspector:
- Type: Assign the tutorial_resource_wood resource type here to define that the tutorial_unit can drop off the wood resources we created.
- Amount: This represents the maximum amount that the unit can have collected from the resource type until it has to drop off or it wouldn't be able to collect and carry more of it. Since the resource collector, [as we configured it previously](xref:Resources_SetupResourceCollectorUnit##5. Resource Collector Tab), collects 10 units of the wood resources every progress period, we can set this field to 50 so that the unit has to drop off its collected resources every 5 collection progress periods.
- Animator Override Controller: To make the tutorial_unit have a unique animation that plays while it is actively dropoff the resource type assigned above, we need to assign it a full animator override controller that replaces the default one only when the dropoff is active (you might want to change the movement animation when the unit is moving to drop off the resources at a dropoff point). We went through creating the animator override controllers for units in the unit creation tutorial in full details. But in short, find a path in the project tab and right click then choose: Create -> Animator Override Controller and assign the animation fields with animation clips to play for the resource collector while it is actively dropping off resources. We will leave it as unassigned to keep things simple for now.
- Enable Object: A child object of the unit can be assigned in this field which will be enabled when the entity is actively dropping off the resource of the type assigned in the above field.
- Enable Audio: This audio clip is played when the player starts dropping off the resource of the type assigned above.
The Total Max Capacity field defines the total amount of the collected resources from all types assigned in the Drop Off Resources. Since we only have one with a maximum local collection capacity of 50 then we can also set this field to 50 or any value higher than that. And finally, when the field Drop Off On Target Available is enabled and the maximum capacity is reached with the unit being idle, the unit will automatically go to the drop off point as soon as it is available.
4. Testing
Load up the tutorial_map scene and play it. Select the pre-placed tutorial_unit instances and right-click on a one of the tree resources in the map.
When one of the collector units reaches 50 wood resources collected, it will then move to the default tutorial_building, which we defined as the resource drop off point. When the collector units reach the drop off point, the resources are then added to the faction. The units then can go back to the tree resource and continue collecting to start the next collection-drop-off cycle.
Download Files
You can download the results of this tutorial here.