- Home /
Newbie mecanim question
I've attached an Animator Controller to my character. Inside the Animator Controller I've made the States I need and attached the animation clips - so far everything is fine.
My animations clips needs to be triggered by events in the game (ex: characterWasHit : boolean) so I need to write a script that trigger the variables in my Animator controller, and this brings me to my question: where do I attach the script that "talks" with the Animator Controller? ... and, can someone point me to a very simple Animator Controller script that JUST sets a boolean value inside the Animator Controller?
Any help will be greatly apprecaited!!
Thanks Jeppe
Answer by JeppeNygaard · Mar 22, 2013 at 07:54 AM
I figured it out - or at least I came to a solution that works
to answer my own question: the script that "talks" with the Animator Controller needs to be attached to the same object the Animator Controller is attached to.
the specific syntax for setting a boolean inside the Animator is: animator.SetBool("variable", true/false);
here is a very simple script creating a function to set a boolean inside an Animator Controller:
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(Animator))]
public class laserCTRL : MonoBehaviour {
protected Animator animator;
void Start ()
{
animator = GetComponent<Animator>();
}
public void fireLaser ()
{
animator.SetBool("laser", true);
}
}
I hope this was helpful
Cheers Jeppe
Answer by zviaz · May 11, 2015 at 02:20 PM
I'll go one step further and maybe provide some information you didn't know.
Animation states (idle, walking, running) are controlled through parameters inside the animator itself. You create the parameters you want (IsIdle, IsWalking, IsRunning) and turn them on/off inside of a script attached to the object in question (AI Enemy for instance). You already appear to have figured out the basic syntax
IsWalking = true;
anim.SetBool ("IsWalking", IsWalking);
Check out the Unity docs on Animation Parameters
Your answer
Follow this Question
Related Questions
Mecanim not playing animation correctly 1 Answer
Mecanim - Transitioning to the same animation in reverse 0 Answers
Is this how you change animator's layer weight? 0 Answers
Mecanim "trigger" parameter behaviour with multiple animator layers 1 Answer
How can I set avatar's particular joint angle via script (mecanim)? 1 Answer