I am using this code to animate and control character movement. I have been working on this for awhile now but am very new to coding so there are probably many mistakes. If I just set the nav agent to control the movement everything works fine the animations will play and blend well but I get a lot of unrealistic foot sliding and such. So I am now trying to use root motion to control the movement but am having no luck. Right now my enemy will play animations when I am close etc. but it is stuck in place. I am at a loss right now so any help is very appreciated.
var senses : EnemySenses; // Enemy senses sript.
var agent : NavMeshAgent; // Navmesh agent.
var target : Transform; // What enemy is targeting.
var chaseSpeed = 11f; // Speed to chase player.
var damping = 5.0f; // Look speed.
var batRange = 10f; // Battle stance range.
var currentSpeed = 0f; // movement speed variable.
var chasing : boolean = false; // Is enemy chasing target.
var angleResponseTime : float = 0.6f;
var angularSpeedDampTime : float = 0.7f;
private var anim : Animator;
private var lastPos : Vector3; // Position of player last frame.
private var player : GameObject; // Player Object.
private var distance; // Distance from enemy to player.
function Awake()
{
target = GameObject.FindGameObjectWithTag("Player").transform;
anim = GetComponent(Animator);
agent.updateRotation = false;
}
function Start()
{
}
function Update()
{
agent.updatePosition = agent.updateRotation = false;
distance = Vector3.Distance(target.position, transform.position);
var currentMovement = transform.position - lastPos;
currentSpeed = currentMovement.magnitude / Time.deltaTime;
lastPos = transform.position;
senses.LookingListening();
AnimationStates();
if(senses.playerHeard == true)
{
HeardPlayer();
}
if(senses.playerSeen == true)
{
SeenPlayer();
}
if(distance < batRange)
{
BattleStance();
}
if(chasing == true)
{
Chase();
}
}
function OnAnimatorMove ()
{
anim.rootPosition = agent.desiredVelocity;
agent.velocity = anim.deltaPosition / Time.deltaTime;
transform.rotation = anim.rootRotation;
}
function AnimationStates()
{
var speed : float;
var angle : float;
angle = FindAngle(transform.forward, agent.desiredVelocity, transform.up);
speed = Vector3.Project(agent.desiredVelocity, transform.forward).magnitude;
var angularSpeed : float = angle / angleResponseTime;
anim.SetFloat("AngularSpeed", angularSpeed);
anim.SetFloat("MoveSpeed", speed);
//var agentVel = agent.desiredVelocity;
//anim.SetFloat("MoveSpeed",agent.speed);
}
function HeardPlayer()
{
if(chasing == true)
{
return;
}
else
{
//LookAt();
}
}
function SeenPlayer()
{
if(chasing == true)
{
return;
}
else
{
Chase();
//LookAt();
}
}
function LookAt()
{
Debug.Log("LookingForPlayer");
var rotation = Quaternion.LookRotation(target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
}
function Chase()
{
agent.SetDestination(target.position);
agent.speed = chaseSpeed;
chasing = true;
}
function BattleStance()
{
//LookAt();
//agent.SetDestination(target.position);
chasing = false;
Debug.Log("Battle Stance");
}
function FindAngle (fromVector : Vector3, toVector : Vector3, upVector : Vector3) : float
{
// If the vector the angle is being calculated to is 0...
if(toVector == Vector3.zero)
// ... the angle between them is 0.
return 0f;
// Create a float to store the angle between the facing of the enemy and the direction it's travelling.
var angle : float = Vector3.Angle(fromVector, toVector);
// Find the cross product of the two vectors (this will point up if the velocity is to the right of forward).
var normal : Vector3 = Vector3.Cross(fromVector, toVector);
// The dot product of the normal with the upVector will be positive if they point in the same direction.
angle *= Mathf.Sign(Vector3.Dot(normal, upVector));
// We need to convert the angle we've found from degrees to radians.
angle *= Mathf.Deg2Rad;
return angle;
}