Ok so I’m new to Unity and Scripting. I found this script in a website and I tweeked it a little bit so that my enemy can idle when it’s out of range and Run when it’s in range and attack when it’s in range. so far my enemy idle when out of range but it doesn’t do the Run animation when it’s following the player. Can someone Rely this with my code fixed? so that it can attack in Range, Run in range and Idle out of range from player? my enemy only has idle, Start (start runing), Run, End (end Running) and Attack.
here is the code i need fixed:
var moveSpeed = 3; //move speed
var rotationSpeed = 3; //speed of turning
var attackThreshold = 3; // distance within which to attack
var chaseThreshold = 10; // distance within which to start chasing
var giveUpThreshold = 10; // distance beyond which AI gives up
var attackRepeatTime = 1; // delay between attacks when within range
private var target : Transform; //the enemy’s target
private var initialSpeed = 0;
private var chasing = false;
private var attackTime = 1;
var myTransform : Transform; //current transform data of this enemy
function Awake()
{
myTransform = transform; //cache transform data for easy access/preformance
}
function Start()
{
target = GameObject.FindWithTag("Player").transform; //target the player
if (!target)
Debug.Log ("ERROR : player not tagged as 'Player'");
// Set all animations to loop
animation.wrapMode = WrapMode.Loop;
// Except our action animations, Dont loop those
animation["End"].wrapMode = WrapMode.Once;
animation["idle"].wrapMode = WrapMode.Once;
animation["Start"].wrapMode = WrapMode.Once;
// Put idle and run in a lower layer. They will only animate if our action animations are not playing
animation["idle"].layer = 1;
animation["Attack"].layer = 1;
animation.Stop();
}
function Update () {
Debug.DrawLine( target.position , myTransform.position , Color.cyan);
// check distance to target every frame:
var distance = (target.position - myTransform.position).magnitude;
if (chasing) {
//rotate to look at the player
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.smoothDeltaTime);
//move towards the player
myTransform.position += myTransform.forward * moveSpeed * Time.smoothDeltaTime;
animation["Run"].layer = 1;
// give up, if too far away from target:
if (distance > giveUpThreshold) {
chasing = false;
animation["idle"].layer = 1;
}
// attack, if close enough, and if time is OK:
if (distance < attackThreshold && attackTime == attackRepeatTime) {
// Attack! (call whatever attack function you like here)
attackTime = attackTime + attackRepeatTime;
// Attack Animation
//animation.CrossFade("attack_animation");
//animation.Play();
}
} else {
// not currently chasing.
// start chasing if target comes close enough
if (distance < chaseThreshold) {
chasing = true;
animation["Run"].layer = 1;
}
if (chasing == true ){
animation.CrossFade("Run");
animation.Play();
}
else if (chasing == false)
{
animation.CrossFade("Start");
animation.Play();
}
}
}