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:
Assets/Resources/Prefabs/Ground
.app
to search for components with that name.Once the script is open, you'll see that it already contains basic variables and methods. Add the following OnPress()
method to handle player movement on left mouse release:
// If a Press event occurs on this object void OnPress(bool pressed) { // If the object is pressed if(pressed) { // If it's a left click, it's a valid movement request validMoveRequest = (UICamera.currentTouchID == -1); } // If the object is released else { // If the movement request is still valid if(validMoveRequest) { // Set destination to the object's pivot Vector3 destination = transform.position; // If the precise bool is checked... if(precise) { // ...Set destination to clicked position destination = UICamera.lastWorldPosition; } // Request player to move with stopping distance PlayerController.Instance.SetDestination( gameObject, destination, stoppingDistance); } } }
Save the script and go back to Unity. We need to configure the ground slightly, as follows:
Assets/Resources/Prefabs/Ground
.Approach On Click
component:0.5
.box
with your keyboard.10
, 0.1
, 10
}.OK. We have now configured the ApproachOnClick
component and its attached Box Collider to catch events.
Hit Unity's play button. When you click on the ground, the player moves precisely to the clicked position, stopping at 0.5 units from the requested position.
This is because, in the preceding code, if precise
is set to true
(which is the case for the Ground
prefab), we set UICamera.lastWorldPosition
as the destination.
Right now, if you click on an interactive element, such as a power source, the player does not try to reach it; that's simply because the object is on top of the ground and doesn't have the ApproachOnClick
component. We can correct this now.
Now that the ground movement is done, let's handle the click on an interactive element. If the player clicks on a power source, for example, we want the player to reach the object.
We'll achieve this by moving the player to the clicked object's pivot point, with a higher stopping distance than with the ground. In the Project view, select both the Resources/Prefabs/PowerSource
and PowerFeed
prefabs:
app
with your keyboard.If you click on an interactive element that does not have the precise Boolean checked, the destination is set to the object's pivot point with the configured stopping distance.
The default values are suitable for the large power source's prefabs. Let's configure a smaller stopping distance for the power feeds since they are smaller objects:
Resources/Prefabs/PowerFeed
.2
.Now, hit Unity's play button. If you click on a power source or feed, the player moves towards it and stops at a reasonable distance to avoid hitting it.
We have a slight problem: if we drag a power source, the player moves towards it when it's dropped as if we clicked on it.
That occurs because we set the destination when the OnPress(false)
event is triggered on the power source. We must cancel the movement if a drag occurs.
Open our ApproachOnClick.cs
script, and add this OnDrag()
method to it:
// If a drag event occurs on this object public void OnDrag(Vector2 delta) { // If the object is draggable... if(isDraggable) { // ...The object moves. Mark as invalid movement request validMoveRequest = false; } }
Hit Unity's play button. That's it. If you move a power source, the player is no longer moving towards it on release!
That's because, in the preceding code, as soon as the object is dragged, validMoveRequest
is set to false
, canceling the move request.
OK, now that we've seen how to catch OnPress()
and OnDrag()
events to move our character and make draggable objects, we can display the player's name above the character.
18.226.251.70