Moving the player

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.

Ground movement

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:

  1. In the Project view, select Assets/Resources/Prefabs/Ground.
  2. Click on the Add Component button in the Inspector view.
  3. Type in app to search for components with that name.
  4. Select Approach On Click and hit Enter or click on it with your mouse.
  5. Right-click on the newly added component's name.
  6. Click on Edit Script to open it.

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:

  1. Select Assets/Resources/Prefabs/Ground.
  2. For its attached Approach On Click component:
    • Check the Precise option.
    • Set Stopping Distance to 0.5.
  3. Click on the Add Component button in the Inspector view.
  4. Type in box with your keyboard.
  5. Select Box Collider and hit Enter or click on it with your mouse.
  6. Set its Size to {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.

Reaching an object

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:

  1. Click on the Add Component button in the Inspector view.
  2. Type in app with your keyboard.
  3. Select Approach On Click and hit Enter or click on it with your mouse.

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:

  1. In the Project view, select Resources/Prefabs/PowerFeed.
  2. Change Stopping Distance of Approach On Click to 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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.93.141