Time for action - script the character

Let's do exactly that! We'll create a new Script, and use what we already know about following the mouse to snap the player character to our mouse movement. Then we'll tell the character which animation cycle to use when he's moving around.

  1. Create a new JavaScript Script. Rename it Character. If you're keeping your project tidy, consider creating a new folder called Scripts and dropping your new script into it.
  2. Open the script and type in this code:
    var lastX:float; // this will store the last position of the character var isMoving:boolean = false; //flags whether or not the player is in motion function Start() { animation.Stop(); // this stops Unity from playing the character's default animation. } function ...

Get Unity 3D Game Development by Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.