O'Reilly logo

Unity 3D Game Development by Example by Ryan Henson Creighton

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required