I’m following scripting from a book. I don’t have all the animations made at the moment, yet I follow the scripts right from the book. Despite (nearly) completing the scripts. None of the animations are playing when I press any of the keys (and I have the animation clips attached to the prefab).
Am I doing something wrong with these scripts?
var runSpeed = 3.0;
var jumpSpeed = 8.0;
var gravity = 20.0;
var rotateSpeed = 4.0;
var crouchSpeed = 0.5;
var isControllable : boolean = true;
//Private, helper variables
private var moveDirection = Vector3.zero;
private var grounded : boolean = false;
private var moveHorz = 0.0;
private var normalHeight = 2.0;
private var crouchHeight = 1.7;
private var rotateDirection = Vector3.zero;
private var isCrouching : boolean = false;
private var isRunning : boolean = false;
//Cache controller so we only have to find it once
var controller : CharacterController;
controller = GetComponent(CharacterController);
var MoaliaStatus : Moalia_Status;
MoaliaStatus = GetComponent(Moalia_Status);
//Move the controller during the fixed frame updates
function FixedUpdate ()
{
if(!isControllable)
Input.ResetInputAxes ();
else
{
if(grounded)
{
//Since we're touching something solid,
//such as the ground, allow movement
//Calculate movement directly from input axes
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= walkSpeed;
moveDirection.y -= gravity * Time.deltaTime;
//Find rotation based upon axes if need to turn
moveHorz = Input.GetAxis("Horizontal");
if (moveHorz > 0) //Right turn
rotateDirection = new Vector3(0, 1, 0);
else if (moveHorz < 0) //Left turn
rotateDirection = new Vector3(0, -1, 0);
else //not turning
rotateDirection = new Vector3(0, 0, 0);
//Jump controls
if(Input.GetButton("Jump"))
{
moveDirection.y = jumpSpeed;
}
//Aplly any boosted speed
if(Input.GetButton("Run"))
{
isRunning = true;
if(MoaliaStatus)
{
moveDirection *= runSpeed;
MoaliaStatus.stamina -= MoaliaStatus.runUsage * Time.deltaTime;
isRunning = true;
}
}
}
//Crouch the controller
if(Input.GetButton("Crouch"))
{
controller.height = crouchHeight;
controller.center.y = controller.height/2 + .25;
moveDirection *= crouchSpeed;
isCrouching = true;
}
if(Input.GetButtonUp("Crouch"))
{
//Reset height and center after ducks
controller.height = normalHeight;
controller.center.y = controller.height/2;
isCrouching = false;
}
if(Input.GetButtonUp("Run"))
{
isRunning = false;
}
//Apply gravity to end jump, enable falling, and make sure he's touching the ground
moveDirection.y -=gravity * Time.deltaTime;
//Move and rotate the controller
var flags = controller.Move(moveDirection * Time.deltaTime);
controller.transform.Rotate(rotateDirection * Time.deltaTime, rotateSpeed);
grounded = ((flags & CollisionFlags.CollidedBelow) != 0);
}
}
//----------------------------------------------------
function IsMoving ()
{
return moveDirection.magniture > 0.5;
}
function IsCrouching ()
{
return isCrouching;
}
function IsGrounded ()
{
return grounded;
}
//Make the script easy to find
@script AddComponentMenu("Moalia/MoaliaCharacterControler")
//////////////////////////////////////////////////////////////////////////////////////
private var nextPlayIdle = 0.0;
var waitTime = 10.0;
var playerController: Moalia_Controller;
playerController = GetComponent(Moalia_Controller);
function Start ()
{
//Set up layers - high numbers recieve priority when blending
animation["Idle"].layer = 0;
//We want to make sure that the (animations) are synced together
animation["Walk"].layer = 1;
animation["Run"].layer = 1;
animation["Crouch"].layer = 1;
animation.SyncLayer(1);
animation["Jump"].layer =5;
//These should take priority over all others
animation["Crouch"].wrapMode = WrapMode.Loop;
animation["Jump"].wrapMode = WrapMode.ClampForever;
//Make sure nothing is playing by accident, then start with a default idle.
animation.Stop();
animation.Play("Idle");
}
function Update ()
{
if(playerController.IsGrounded ())
{
animation.Blend("Jump", 0, 0.2);
//If Running
if(playerController.IsRunning ())
{
animation.CrossFade("Run", 0.5);
nextPlayIdle = Time.time + waitTime;
}
else if(playerController.IsCrouching ())
{
animation.CrossFade("Crouch", 0.2);
nextPlayIdle = Time.time + waitTime;
}
//Fade in Walk
else if(playerController.IsMoving ())
{
animation.CrossFade("Walk", 0.5);
nextPlayIdle = Time.time + waitTime;
}
//Fade out roll and fast roll else
else
{
animation.Blend("Run", 0.0, 0.3);
animation.Blend("Walk", 0.0, 0.3);
animation.Blend("Crouch", 0.0, 0.3);
if(Time.time > nextPlayIdle)
{
nextPlayIdle = Time.time + waitTime;
PlayIdle();
}
}
//Saftey test for idle
if(Input.anyKey)
{
nextPlayIdle = Time.time + waitTime;
}
}
}
function PlayIdle ()
{
animation.CrossFade("Idle", 0.2);
}
@script AddComponentMenu("Player/Moalia'AnimationManager")
Book: Game Development With Unity Second Edition
Update: forgot to attach the script. Now I have a load of these errors:
“MissingMethodExceptioni: Moalai_Controller.IsRunning
Boo.Lang.Runtime.Dynamicdispatching.MedfthodDispacherFactory.ProduceExtensionDispacher ()” (@ line 32, Moalia_Animation)