Time for action – scripting 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.
Create a new JavaScript. Rename it
Character
. If you're keeping your project tidy, consider creating a new folder calledScripts
and dropping your new script into it.Open the script and type in this code beneath the
#pragma
strict line: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 Update() { transform.position.x = (Input.mousePosition.x)/20; }
So far, all this should look very familiar. We're using the same type of commands we used with the keep-up game to make the GameObject follow the...