The blue sphere represents our player. We would like it to move when the ground—or an interactive element—is clicked on. The PlayerController.cs
script attached to the Player
GameObject already has a method to handle movement: SetDestination()
.
We can use NGUI events to catch the OnPress()
event on our ground and interactive elements and call the player's SetDestination()
method to reach the clicked position.
The ApproachOnClick.cs
script included in the package we downloaded will help us with this. It simply has to be added to any object that needs to be approached on left-click. Here's how you can accomplish this task:
In the Project view, select
Assets/Resources/Prefabs/Ground
.Click on the Add Component button in the Inspector view.
Type in
app
to search for components with that name.Select Approach On Click and hit Enter or click on it with your mouse.
Right-click on the newly added component's name.
Click on Edit Script to open it.
Once the script is open,...