- Home /
learn to scripting parameter in Mecanim
I am beginer in unity scripting, I just learn new mecanim system..
I have a 3 animations : walk stright, left and the right that is ready blend in mecanim
and I am using "directiions" and "speed" parameter to control it, (-1 for right, 0 for stright and 1 for left),
so I need my character to follow other agent or going to destination object, but I can't figure out how to script those parameter in order to make my avatar facing my stright to destination object.
thank you,
any help would be appreciated
Answer by Hyperion · Jan 06, 2013 at 01:54 AM
Everything in Unity-Javascript.
First, you have to make the character turn towards the object.
//looking at Target
var rotate = Quaternion.LookRotation(Target.position-transform.position);
transform.rotation=Quaternion.Slerp(transform.rotation,rotate,Time.deltaTime*damp);
Remember to declare the variable Target as Transform. This code simply turns the user towards target.
Then, make the character move towards target. I made a variable moveSpeed. Do not do it this way if your walking animation has Root Motion. Just set the speed parameter (explained at end of answer)
transform.Translate(Vector3.forward *moveSpeed* Time.deltaTime); //move forward
If you also want to set parameter "Speed", declare a variable at the start of the Update function (whatever you want to call it) like this:
//Initializing the animator component
var YourAnimatorName: Animator = GetComponent(Animator);
Then, you can do
YourAnimatorName.SetFloat("Speed",YourSpeed);
Set float is only for speed, if it's a boolean, do SetBool, and so on. Put this where you get your Character to move.
Hope this helps,
Hyperion
If this answer was helpful, could you please accept it and vote it up? Thanks. That way I can provide more help in the future.
@Hyperion i'm getting error like "Parameter 'TestInput' does not exist", have you any idea what i'm doing wrong?
Your answer
Follow this Question
Related Questions
Random float parameter for idle animations 1 Answer
unity freezes when running sprinting animation 0 Answers
Changing variable value from Mecanim? 2 Answers
Animation will not Return to Proper State 2 Answers
how to set mecanim parameters for an ai? 0 Answers