- Home /
Mecanim walk to target position
Hi,
I am trying to make my Mecanim character to walk to a position in world space, in some event. How would I make the character walk to that position, and follow it even if the position was changed?
Thanks in advance!
Answer by infinitypbr · Oct 05, 2013 at 12:36 AM
Since you mention Mecanim, I'm guessing you want the animation 'walk' to play while the character moves?
First you'll need to make the character move automatically. For that, define a variable Vector3 with the position you'd like the character to walk to. (You could use a NavMeshAgent as well).
To ensure that Mecanim responds with the proper animation, you'll need to get the delta movement of the character, and delta rotation. For that, save the variable Vector3 Position of the character, and compare that with the last one...
Something like:
var currentPos : Vector3; // Current position of the player
var playerAnimator : Animator; // Assign this in the inspector
function Update () {
if (currentPos) // won't run on the first frame if currentPos is null)
{
var currentVelocity = Vector3.Distance(transform.position, currentPos); // gets the amount the character moved in the last frame
currentPos = transform.position; // reset
playerAnimator.SetFloat("velocity", currentVelocity); // Pass currentVelocity to Animator
}
}
Do something similar for the rotation. This way the animations, controlled by Velocity and a variable for turning amount (X/Y, basically), can be adjusted when the player isn't pressing controller buttons, and the animations will play properly.
Keep in mind RootMotion won't work with this setup, since the actual movement of the player is being controlled by the NavMesh or through direct code.
Thank you for the effort, however, I can see an error in the code, transform.position returns a Vector3, currentPos is a Vector3, and Vector3 - Vector3 = Vector3. You are passing it to $$anonymous$$echanim as a float.
The Nav$$anonymous$$esh solution could be doable, but I wanted to avoid it, so that I don't need to generate it simply for the character to be moved on occasion. It feels like a waste, unless I end up using it for the AI. A simple solution would be to just turn the player towards the position in world space on the Y axis and trigger the walk animation, but the thing is, I want the character to do more than just walking. The character would walk towards a door, stop and open it, and actually move closer to the door while it is being opened, so that I can guide the I$$anonymous$$ to the door handle accurately. (I have all the positions defined.) If I were to move it through code, I'd have to calculate how fast the player transform is moving, and what speed the animation should be playing at.
I have updated the question's tags with c#. I prefer C# over Unity-JavaScript.
You're right -- was supposed to be Vector3.Distnace. I've updated the code. Also added the line required to reset the currentPosition variable for the next frame.
Someone with more knowledge would have to explain how to do the door opening etc, as I've never done anything like that.
If you want the character to simply move forward, then using the LookAt function per frame will help.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
C# move y position of object not working 2 Answers
Move block to click 1 Answer
Distribute terrain in zones 3 Answers