Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Punkjim420 · Aug 13, 2012 at 04:46 PM · rigidbodydamageenemy aiattackhealth

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);

}

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Punkjim420 · Aug 13, 2012 at 04:14 PM 0
Share

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..?

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

  1. 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.

  2. 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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Punkjim420 · Aug 20, 2012 at 07:21 AM 0
Share

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.

avatar image
0

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
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Animation + Attack when in range 1 Answer

I cant get my projectiles to do damage or keep them from going through my player 1 Answer

Melee Damage script by collision 2 Answers

One enemy triggers all the enemies 2 Answers

Enemy health drops but not if first enemy lives, how do i change that? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges