Book Image

Learning NGUI for Unity

By : Charles Bernardoff (EURO)
Book Image

Learning NGUI for Unity

By: Charles Bernardoff (EURO)

Overview of this book

Table of Contents (17 chapters)
Learning NGUI for Unity
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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,...