- Home /
incomplete Enemy Ai (move/attack on sight) js script
Im trying to make my enemy ai. I want it to run to me if im withing a set range from it(var chaseThreshold). I want it to stop running towards me, once it reaches the attack range(attackThreshold). Lastly, once it reaches the Attack range, i want it to send a message to the Player Object that deals damage.
When i test the script as is in unity, the enemy does not move. If i remove the bit telling my enemy to stop when it reaches attack range(attackThreshold) it moves towards my player and never stops, sometimes sends my player flying backwards on collision. I cant seem to find the proper way to my enemy to walk towards my player and stop when it reaches attack range at the same time. Also, my send message to apply damage does not get recieved. Help with either issue would be greatly apprieciated.
Enemy Ai.js:
var target : Transform; //enemies target var moveSpeed = 3; var rotationSpeed = 3; var attackThreshold = 10; //distance in which to attack var chaseThreshold = 20; //distance to chase var giveUpThreshold = 21; //how far you go before enemy quits chase var attackRepeatTime = 1; //attack delay var damage = 5; private var chasing = false; private var attackTime = Time.time; var myTransform : Transform; //current transform data of the enemy
function Awake(){ myTransform = transform; //cache transform data for easy access/performance }
function Start(){ target = GameObject.FindWithTag("Player").transform; //target the player }
function Update(){ var distance = (target.position - myTransform.position).magnitude; if(chasing) { myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime; //move to player
if(distance > giveUpThreshold || distance < attackThreshold) { //give up if too far away
chasing = false;
}
//attack if close and timer is ready
if(distance < attackThreshold && Time.time > attackTime) {
//attack: do your attack here.
BroadcastMessage("PlayerDamage", damage, SendMessageOptions.DontRequireReceiver);
attackTime = Time.time + attackRepeatTime;
}
else {
//not currently chasing
//start chasing if target comes close
if(distance < chaseThreshold) {
chasing = true;
}
}
} } ***Player Status.js:*** //Goes on Player Object. public var playerCurHp : int; public var playerMaxHp : int; public var playerCurMp : int; // public var playerMaxMp : int; public var playerCurExp : int; public var playerMaxExp : int; public var playerLevel : int; var statusTexture: Texture; var hitDelay : float = 0.75; private var nextHitAllowed : float;
playerCurHp = 99999; playerMaxHp = 99999; playerCurMp = 99999; playerMaxMp = 99999; playerCurExp = 0; playerMaxExp = 250;
function PlayerDamage (damage : int){ playerCurHp -= damage;
//Debug.Log(playerCurHp);
if(playerCurHp > playerMaxHp){ playerCurHp = playerMaxHp; } if(playerCurHp < 0){ playerCurHp = 0; } if(playerCurHp < 1){ //die // Die(); //Debug.Log("YOU DIED!"); }
//function Die () { //Application.LoadLevel (Application.loadedLevel);
}
function OnGUI(){ GUI.Label(Rect(50,10,150,100), statusTexture); GUI.Label(Rect(50,20,150,100), "HP " + (playerCurHp) + "/" + (playerMaxHp)); GUI.Label(Rect(50,35,150,100), "MP " + (playerCurMp) + "/" + (playerMaxMp));
}
i would still appreciate help. though iv changed the script a bit... i could update if anyone would like to see it, or use pastebin.
Your answer
![](https://koobas.hobune.stream/wayback/20220613075339im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Move Camera According to Mouse Movement While Button is Pressed 1 Answer
Enemy Movement Problem! 1 Answer
UnityScript How to Detect Movement from FPS Controller 0 Answers
Line not working in C#? 1 Answer
Android OnMouseDown character movement 2 Answers