Scripting for character movement
This script is an example of a character control script that uses the Input
class and CharacterController
class' Move
command to create character movement by manipulating a Vector3
variable.
Deconstructing the script
To test how much you have learned so far, take a look at the script in its entirety first, to see how much you can understand, then read on to see each part deconstructed.
Full script (Javascript)
The full deconstruction of the script is as follows:
var speed : float = 6.0; var jumpSpeed : float = 8.0; var gravity : float = 20.0; private var moveDirection : Vector3 = Vector3.zero; private var grounded : boolean = false; function FixedUpdate() { if (grounded) { moveDirection = Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); moveDirection = transform.TransformDirection(moveDirection); moveDirection *= speed; if (Input.GetButton ("Jump")) { moveDirection.y = jumpSpeed; } } moveDirection.y -=...