I searched and searched and found similar unanswered questions. I would like to use the Mecanim to animate my AI. I have an AI script but its not working. I found this script and changed what the enemy did. Originally it turned colors only, so I added animation to it instead but I think I did it wrong. Please help. (I have a character controller and an animator controller with these animations on it. attached to my enemy also)
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 TheDammage = 40;
private var attackTime : float;
var controller : CharacterController;
var gravity : float = 20.0;
private var MoveDirection : Vector3 = Vector3.zero;
function Start ()
{
attackTime = Time.time;
animation.Play("idle01");
}
function Update ()
{
if(RespawnMenuV2.playerIsDead == false)
{
Distance = Vector3.Distance(Target.position, transform.position);
if (Distance < lookAtDistance)
{
lookAt();
}
if (Distance > lookAtDistance)
{
animation.Play("walk02");
//renderer.material.color = Color.green; //this worked
}
if (Distance < attackRange)
{
animation.Play("attack01");
//attack();
}
else if (Distance < chaseRange)
{
animation.Play("run");
//chase ();
}
}
}
function lookAt ()
{
animation.Play("caution");
//renderer.material.color = Color.yellow; //this worked
var rotation = Quaternion.LookRotation(Target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
}
function chase ()
{
animation.Play("run");
//renderer.material.color = Color.red; // this worked
moveDirection = transform.forward;
moveDirection *= moveSpeed;
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
function attack ()
{
if (Time.time > attackTime)
{
Target.SendMessage("ApplyDammage", TheDammage);
Debug.Log("The Enemy Has Attacked");
attackTime = Time.time + attackRepeatTime;
}
}
function ApplyDammage ()
{
chaseRange += 30;
moveSpeed += 2;
lookAtDistance += 40;
}
↧