- Home /
How to Make my Character Run?
Hi, I'm using MECHANIM and I'm having some issues. Essentially, when I press LeftShift, I want to be able to toggle the boolean "IsRunning" (my running animation boolean in MECHANIM) to true, in the else statement set it to false. Buuuut, of course, since I have no idea what I'm doing quite yet, I need your help. This is what I got:
//SPRINTING
if(controller.velocity.magnitude > 0 && Input.GetKey(KeyCode.LeftShift))
{
theAnimator.SetBool("IsRunning", true);
}
else
{
theAnimator.SetBool("IsRunning", false);
}
Just to let you know, I did this at the beginning of the script:
var theAnimator : Animator;
Well that's about it, hopefully you can help me out on this one!
(NOTE: The script gives NO errors, but when I press LeftShift, nothing happens)
Hey, I put some logs in there, and it wasn't reading any output onto the console..weird.
Answer by FrostyNomad · Nov 26, 2014 at 12:43 AM
You may not be getting the Animator object. Your script should look something like this:
var theAnimator : Animator;
function Start() {
theAnimator = gameObject.GetComponent("Animator");
}
function Update() {
if(controller.velocity.magnitude > 0 && Input.GetKey(KeyCode.LeftShift))
{
theAnimator.SetBool("IsRunning", true);
Debug.Log("IsRunning is set to true");
}
else
{
theAnimator.SetBool("IsRunning", false);
Debug.Log("IsRunning is set to false");
}
}
Also realize that Update is called each frame so, unless the shift key is being held down, IsRunning will be set back to false.
Hi, I made a new script and pasted that in there, but it still does not work. I through some Debug.Log() statements and nothing was appearing in the console. Have any idea?
I updated my answer to include a couple Debug.Log statements. Checklist: 1. $$anonymous$$ake sure your script builds. 2. $$anonymous$$ake sure you're attaching the script to a GameObject in your scene 3. $$anonymous$$ake sure you have your Console filter toggles set http://i.imgur.com/0XhVtF9.gif
Hey - I followed your instructions and it worked perfectly. Thank you so much for your help. $$anonymous$$udos!