- Home /
How do I animate an 'enemy' NPC?
I've been creating a simple survival game, and have a basic AI scripted, but I now wish to apply this script to an actual animated model, rather than the capsule I'm using now. I'm a real beginner here, so if anyone can offer help, please understand that I know little about code, I'm just piecing bits together from what I've found!
The model I'm using is here on the asset store. I have imported the skeleton model into Unity, and seen that it has animations included on the import list, but I have no idea how to take this any further in terms of actually making it work with my AI.
I have also attached my current AI script below. If possible I would like to maintain the script behaviour as much as possible, but removing the colour changes and introducing the animated elements where appropriate.
var Distance;
var Target : Transform;
var lookAtDistance = 25.0;
var chaseRange = 15.0;
var attackRange = 1.5;
var moveSpeed = 5.0;
var Damping = 6.0;
var attackRepeatTime = 1;
var Damage = 40;
private var attackTime : float;
var controller : CharacterController;
var gravity : float = 20.0;
private var MoveDirection : Vector3 = Vector3.zero;
function Start ()
{
attackTime = Time.time;
}
function Update ()
{
Distance = Vector3.Distance(Target.position, transform.position);
if (Distance < lookAtDistance)
{
lookAt();
}
if (Distance > lookAtDistance)
{
renderer.material.color = Color.green;
}
if (Distance < attackRange)
{
attack();
}
else if (Distance < chaseRange)
{
chase ();
}
}
function lookAt ()
{
renderer.material.color = Color.yellow;
var rotation = Quaternion.LookRotation(Target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
}
function chase ()
{
renderer.material.color = Color.red;
moveDirection = transform.forward;
moveDirection *= moveSpeed;
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
function attack ()
{
if (Time.time > attackTime)
{
Target.SendMessage("ApplyDamage", Damage);
Debug.Log("The Enemy Has Attacked");
attackTime = Time.time + attackRepeatTime;
}
}
function ApplyDamage ()
{
chaseRange += 30;
moveSpeed += 2;
lookAtDistance += 40;
}
Many thanks in advance for any help!
inside you script where you move the toon you want to use a code like animation.crossfade(name of animation);