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
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