- Home /
Enemy not doing damage to player
I have two different scripts for the player and the enemy. the enemy script is
var target : Transform; //the enemy's target 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 = 20; // distance beyond which AI gives up var attackRepeatTime = 1; // delay between attacks when within range
private var chasing = false; private var attackTime = Time.time;
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 }
function Update () {
// 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.deltaTime);
//move towards the player
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
// give up, if too far away from target:
if (distance > giveUpThreshold) {
chasing = false;
}
// attack, if close enough, and if time is OK:
if (distance < attackThreshold && Time.time > attackTime) {
// Attack! (call whatever attack function you like here)
}
attackTime = Time.time+ attackRepeatTime;
}
else {
// not currently chasing.
// start chasing if target comes close enough
if (distance < chaseThreshold) {
chasing = true;
}
}
}
and the player script is
var maximumhitPoints = 100; var playerhitpoints= 100; var isDead = false;
function ApplyDamage (damage:float) { if (playerhitpoints < 0) return;
//apply damage playerhitpoints --;
if (playerhitpoints <= 0) { Die(); } }
function OnGUI() { if (isDead) { GUI.Box(Rect(10, 10, 150, 100), "You're dead"); if(GUI.Button(Rect(5, 5, 15, 10), "Click here")) { isDead = false; } } }
function Die() { if(playerhitpoints <= 0) isDead=true; }
when the player dies a pop up window pops up and says your dead and that works and the enemy follows you and all that but I can't get it to do any damage to me. I think the problem is where it says insert attack function here in the enemy script and I've tried doing an on collision function and a applydamage function but I keep getting the expect ( error. any ideas?
Answer by Tetrad · Nov 16, 2010 at 05:07 PM
You don't want to do a collision function there. The code is set up to check for minimum distances already.
Just call the ApplyDamage function on the player where that attack comment is.
Here's one way to potentially do it (untested):
target.SendMessage( "ApplyDamage", 10 );
and i would put that where the attack function goes? and the 10 that's the value of the damage done?
that doesn't seem to do it. The window doesn't pop up after he attacks me. I'm pretty sure he's not actually doing any damage.
ok so i finally have goten him to do damage to me. The problem i have right now is that he will only do damage once takeing my health to 99 and he wont attack if the attack threshold is 3 or less. any ideas?
Finaly got it! the Time.time is always equal to attacktime as we said in the private variable up top. so i changed attack time to attackrepeattime and voiala i can die! i've never been so excited to die! hahah