How to steer character by path using Root Motion?
I have a animated character in Unity3D and I've implemented A* pathfinding algorithm. So, I have a path that contains Vector3 nodes and I want to steer my character using Mecanim Root Motion. What is the best priactice to implement character steering by path and controlls him with such parameters as float Speed, AgularSpeed, Direction.
I've found this script implemented by Unity Technologies:
using UnityEngine;
using System.Collections;
public class Locomotion
{
private Animator m_Animator = null;
private int m_SpeedId = 0;
private int m_AgularSpeedId = 0;
private int m_DirectionId = 0;
public float m_SpeedDampTime = 0.1f;
public float m_AnguarSpeedDampTime = 0.25f;
public float m_DirectionResponseTime = 0.2f;
public Locomotion(Animator animator)
{
m_Animator = animator;
m_SpeedId = Animator.StringToHash("Speed");
m_AgularSpeedId = Animator.StringToHash("AngularSpeed");
m_DirectionId = Animator.StringToHash("Direction");
}
public void Do(float speed, float direction)
{
AnimatorStateInfo state = m_Animator.GetCurrentAnimatorStateInfo(0);
bool inTransition = m_Animator.IsInTransition(0);
bool inIdle = state.IsName("Locomotion.Idle");
bool inTurn = state.IsName("Locomotion.TurnOnSpot") || state.IsName("Locomotion.PlantNTurnLeft") || state.IsName("Locomotion.PlantNTurnRight");
bool inWalkRun = state.IsName("Locomotion.WalkRun");
float speedDampTime = inIdle ? 0 : m_SpeedDampTime;
float angularSpeedDampTime = inWalkRun || inTransition ? m_AnguarSpeedDampTime : 0;
float directionDampTime = inTurn || inTransition ? 1000000 : 0;
float angularSpeed = direction / m_DirectionResponseTime;
m_Animator.SetFloat(m_SpeedId, speed, speedDampTime, Time.deltaTime);
m_Animator.SetFloat(m_AgularSpeedId, angularSpeed, angularSpeedDampTime, Time.deltaTime);
m_Animator.SetFloat(m_DirectionId, direction, directionDampTime, Time.deltaTime);
}
}
I can controll character with something like this:
protected void SetupAgentLocomotion()
{
if (agent.Arrive)
{
locomotion.Do(0, 0);
}
else
{
float speed = agent.desiredVelocity.magnitude;
Vector3 velocity = Quaternion.Inverse(transform.rotation) * agent.desiredVelocity;
float angle = Mathf.Atan2(velocity.x, velocity.z) * 180.0f / 3.14159f;
locomotion.Do(speed, angle);
}
}
But I don't know how to implement agent. I think I need something like NavMeshAgent with desiredVelocity.
PS. I can't use Unity's NavMesh because I have to use my own A* pathfinder. It's more acurate for me.
Your answer
Follow this Question
Related Questions
How to predict humanoid retargeting root y offset ? 0 Answers
Movement following the A* algorithm 0 Answers
Sliding issue with mecanim 0 Answers
Navmesh interfering with mecanim jump animation (root motion) 1 Answer
MatchTarget Rotating Character 0 Answers