Improving Upon Selection Highlighting in Our Game — Part 2
This article is about the development process of our first game. During the process, we are learning how to use Unity & Blender from scratch to create everything you see in the article, enjoy!
Introduction
Welcome to part 2 of improving upon selection highlighting in our game, this time I will be working on the remaining improvements from where we left off last time;
- The arrow that highlights that the water pump is selected is currently placed too low
- The arrow does not disappear when walking away from the water pump
- The arrow should only appear for the water pump when carrying the watering can
Implementing a Position Offset for the Arrow
As you might remember, the arrow that highlights that the water pump is selected is spawned too low(actually inside the water pump) and makes it hard to see, so the first thing we are going to do is to modify the code to spawn the arrow higher when a water pump is selected.
We will start with implementing a variable for defining the height offset for the arrow called arrowHeightOffset
in ItemSelector.SpawnSelectionArrow()
and applying that to the position where the arrow is instantiated.
In the Unity editor, I will try with the value 3
for the offset of the water pump and see if the value is good or not.
After adjusting it to 3.5
instead it seems to be the perfect height, that was easy!
I also noticed that the spade has its arrow placed incorrectly, so we can add another offset for the Z-axis for that so that the arrow is placed in the middle.
Bugfix — The Arrow Does Not Disappear When Walking Away From the Water Pump
Right now, the arrow doesn’t disappear when walking away from the water pump(as it should) unless you walk in range of something else that also can be selected.
Here is the function call to remove the selection arrows:
I’ll just change it to include the layer that the water pump is using to see if it makes the arrows disappear when you go out of range:
The Arrow Should Only Appear for the Water Pump When Carrying a Watering Can
The next thing to fix is to make sure that the arrow that is spawned above the water pump only appears when carrying the watering can. This is to make it more clear to the player when you can use the water pump or not.
First, we need to check if the player is carrying the watering can, and luckily I have already created an appropriate variable for another purpose that we can use. The variable is called equippedWateringCanGameObject
which checks if a watering can is equipped.
To give you a refresher from the previous article, this is what the function call to add an arrows looks like:
I’m going to add checks for the following 2 things to make sure that the arrow is shown at the right time:
- Is the selected item a water pump?
- Is the player carrying a watering can?
For the first if case
, we can use the tag on the game object to check what type of item is selected.
As a proof of concept, I’ll just start with detecting if the if case
for detecting the type of item works first.
Since that seems to work, let’s add code to check if the player is carrying a watering can before instantiating the arrow.
The following if case
might look a bit complicated, but it states the following to make sure that the arrow is instantiated in appropriate situations.
If the the selected item is not a water pump AND you are not carrying any item — OR — if the item is a water pump AND you are carrying a watering can”
This also seems to have worked, so that makes 3 out of 3 completed! ✅
Conclusion
This marks the end of part 2, with most likely only one part left before we continue to finish the vehicle system.
In the last part of improving the selection highlighting system, I’m going to implement the functionality to press Tab
and cycle through all items in range to be able to select the right one.
See you in part 3!