- Home /
How to set an Animator Boolean with JavaScript
So far I have this script, but when I attempt to have the Shift key trigger a boolean to toggle a run animation, and have it toggled back off once the key is released, I get the error "'SetBool' is not a member of 'System.Type'. Can Anyone help? I have a picture added for further clarity. Please keep in mind, that I know almost nothing about how the Animator Works in the first place, so please keep any explanations to a "So-Easy-A-Caveman-Could-Do-It" level.
http://imageshack.us/f/22/pjs5.png/
#pragma strict
var TheAnimator = Animator;
function Update ()
{
if (Input.GetKeyDown(KeyCode.LeftShift) || Input.GetKeyDown(KeyCode.RightShift))
{
Sprints();
}
else
{
NoSprints();
}
}
function Sprints()
{
TheAnimator.SetBool("IsRunning", true);
}
function NoSprints()
{
TheAnimator.SetBool("IsRunning", false);
}
Answer by BeretTheRed · Jun 20, 2014 at 09:45 AM
Try do it this way:
#pragma strict
var ThePlayer : GameObject; // Game object that have animator
ThePlayer = gameObject.Find("game_object_name");
var TheAnimator = Animator;
TheAnimator = ThePlayer.GetComponent(Animator);
function Update ()
{
if (Input.GetKeyDown(KeyCode.LeftShift) || Input.GetKeyDown(KeyCode.RightShift))
{
Sprints();
}
else
{
NoSprints();
}
}
function Sprints()
{
ThePlayer.TheAnimator.SetBool("IsRunning", true);
}
function NoSprints()
{
ThePlayer.TheAnimator.SetBool("IsRunning", false);
}
Your answer
Follow this Question
Related Questions
Animations not Transitioning 0 Answers
why I have to anim.getComponent in update() function when I had done in Start () function 2 Answers
How can I create a home position where my character returns to after every animation? 0 Answers
How can I change my AnimatorState instantly? 1 Answer
Animation Mecanim - Parts of model displaces after animating 0 Answers