- Home /
rigidbody enemy goes only forward, no damage delay.
I am attempting to create an enemy ai script, currently the enemy only moves forward on the z axis. when i enter scene, function(ApplyDamage); gets called(its on player : Transform), but it calls it so much, hp is zero upon starting game. hp is set to 100, and the enemy moved in other directions(following player correctly) if it had no attack function, but now, i cant get both to work at once.
var enemyMaxHp = 100; var enemyCurHp =100; var target : Transform; 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 = 2; //attack delay public var damage; private var chasing = false; private var attackTime; myTransform = transform; //cache transform data for easy access/performance var myTransform : Transform; //current transform data of the enemy var percentMultiplier = 0.01; var onePercentHp : float; var curPercentHP : float; var scaledLength : float; var floatingHpBar : Transform; var floatingHpBarZ : float; var floatingHpBarY : float; var distance; var attackNow : boolean;
function Awake(){ attackTime = Time.time; myTransform = transform; //cache transform data for easy access/performance }
function Start(){ target = GameObject.FindWithTag("Player").transform; //target the player
target = gameObject.GetComponent(Transform);
// This is equivalent to:
target = gameObject.transform;
}
function Update () { attackTime = Time.time; var distance = (target.position - myTransform.position).magnitude; onePercentHp = enemyMaxHp / 100; curPercentHp = enemyCurHp / onePercentHp; scaledLength = curPercentHp percentMultiplier; floatingHpBar.transform.localScale = Vector3(scaledLength 1.5, floatingHpBarY, floatingHpBarZ);
if (enemyCurHp > enemyMaxHp){
enemyCurHp = enemyMaxHp;
}
if (enemyCurHp < 0){
enemyCurHp = 0;
}
//if (enemyCurHp = 0){
//destroy object
//}
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) { //give up if too far away
chasing = false;
}
//not currently chasing
//start chasing if target comes close
if(distance < chaseThreshold || distance > attackThreshold) {
chasing = true;
}
if(distance < attackThreshold && Time.time > attackTime){
chasing = false;
}
if(distance < attackThreshold){
Attack();
attackTime = Time.time + attackRepeatTime;
}
}
function Attack () {
// Calls the function ApplyDamage with a value of 5
gameObject.FindWithTag("Player").SendMessage ("ApplyDamage", 5.0);
}
By the way, Debug says my "Look rotation viewing vector is zero, my rotation on my enemy is 0. Enemy might be forced to not rotate by something..?
Answer by sman_47o · Aug 14, 2012 at 06:29 PM
id say you should probably do a couple things to make finding out why this is happening much much easier.
first off don't have such a huge update function it can kill performance (especially with say 100 enemies) and it makes it hard to find out what is causing the problem, instead have the update function call other functions so that it can continue on its merry way wile the other functions do their work.
even if you don't split the update function at least do this add a debug log to each possible thing that could go wrong that way you know exactly whats happening when it should or more importantly when it shouldn't.
I can't see any direct problems off the top of my head but if you were to set your code up like this:
function Update () {
attackTime = Time.time;
var distance = (target.position - myTransform.position).magnitude;
onePercentHp = enemyMaxHp / 100;
curPercentHp = enemyCurHp / onePercentHp;
scaledLength = curPercentHp * percentMultiplier;
floatingHpBar.transform.localScale = Vector3(scaledLength * 1.5, floatingHpBarY, floatingHpBarZ);
if (enemyCurHp > enemyMaxHp){
enemyCurHp = enemyMaxHp;
Debug.Log("enemy has full health");
}
if (enemyCurHp < 0){
enemyCurHp = 0;
Debug.Log("enemy has no health");
}
//if (enemyCurHp = 0){
//destroy object
//Debug.Log("enemy is dead");
//}
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
Debug.Log("is chasing");
}
if(distance > giveUpThreshold) { //give up if too far away
chasing = false;
Debug.Log("gave up enemy is too far away");
}
//not currently chasing
//start chasing if target comes close
if(distance < chaseThreshold || distance > attackThreshold) {
chasing = true;
Debug.Log("should be chasing target is close");
}
if(distance < attackThreshold && Time.time > attackTime){
chasing = false;
Debug.Log("shouldn't be chasing target i got tired of it");
}
if(distance < attackThreshold){
Attack();
attackTime = Time.time + attackRepeatTime;
Debug.Log("should be attacking target is in range");
}
}
function Attack () {
// Calls the function ApplyDamage with a value of 5
gameObject.FindWithTag("Player").SendMessage ("ApplyDamage", 5.0);
Debug.Log("is attacking");
}
wile this won't fix your problem it will probably highlight it and make it much easier to fix. If you still have are having problems come tell us what parts aren't firing right and post it and people will be able to focus more on that one instance. By the way i like your style of coding you do stuff in a no frills sorta way which will help you in the end have a faster running game.
thanks, i ran a lot more checks like you have shown above, and i eventually found my problem, i dont recall what it was sadly, because i solved it shortly after your reply, and its been 5 days now... i did not expect to get a reply to this post. I will show my working code however to let you see what i did if your interested, and i attempted shortening the Update as you suggested. making chase a function and removing a few non-needed extras. may i ask what you mean when you say i have a no frills way of coding? id love to understand what you ment by that a little more, it may improve my coding even more.
Answer by Punkjim420 · Aug 20, 2012 at 11:55 AM
This is my final code. i shortened the update for performance, and re wrote it in a few places.. though i dont remember which directly caused the error. However it works fine written this way.
var enemyMaxHp = 100;
var enemyCurHp =100;
var target : Transform;
var moveSpeed = 10;
var rotationSpeed = 10;
var attackThreshold = 10; //distance in which to attack
var giveUpThreshold = 51; //how close you get before enemy start chase
var attackRepeatTime = 100; //attack delay
public var damage;
private var chasing = false;
var attackTime;
myTransform = transform; //cache transform data for easy access/performance
var myTransform : Transform; //current transform data of the enemy
var distance;
var idle;
function Awake(){
attackTime = Time.time;
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 (enemyCurHp > enemyMaxHp){
enemyCurHp = enemyMaxHp;
}
if (enemyCurHp < 0){
enemyCurHp = 0;
}
//if (enemyCurHp = 0){
//destroy object
//}
if(chasing){
Chase();
}
if(distance > giveUpThreshold) { //give up if too far away
chasing = false;
idle = true;
}
if(idle){
//Idle animation.
}
//not currently chasing
//start chasing if target comes close
if(distance < giveUpThreshold) {
chasing = true;
}
if(distance < attackThreshold){
Attack();
}
}
function ApplyEnemyDamage (damage : float) {
enemyCurHp = enemyCurHp - damage;
Debug.Log(enemyCurHp);
}
function Attack () {
chasing = false;
if(attackTime < Time.time){
// Calls the function ApplyDamage with a value of 5
gameObject.FindWithTag("Player").SendMessage ("ApplyDamage", 5.0);
attackTime = Time.time + attackRepeatTime;
}
}
function Chase(){
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
}