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 lolnoob · Apr 09, 2014 at 11:10 PM · bugaienemy

Bug with my enemyAI

I got a simple basic enemyAI script of the unityanswers, and i need some help with it it is for a First Person RPG Adventure game i am working on. When the enemy chases you lose health, (heaps of health), When you should only lose one health, when the enemy is actually near you and attacking you. Here is the enemyAI script

var Player : Transform; var MoveSpeed = 4; var MaxDist = 10; var MinDist = 5;

function Start () {

}

function Update () { transform.LookAt(Player);

 if(Vector3.Distance(transform.position,Player.position) >= MinDist){
 
      transform.position += transform.forward*MoveSpeed*Time.deltaTime;
 
 
 
      if(Vector3.Distance(transform.position,Player.position) <= MaxDist)
          {
             GameObject.FindGameObjectWithTag("Player").GetComponent(HealthBar).AdjustCurrentHealth(-1);
             animation.Play("attack1");

}

} }

Comment
Add comment · Show 3
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 lolnoob · Apr 09, 2014 at 10:29 PM 0
Share

Please reply Asap, I really need help with this... You will also get credit for the game, when its released;

avatar image Nightdr · Apr 09, 2014 at 11:44 PM 0
Share

Can I ask what is wrong here? Is the the damage not being applied? Is the enemy not going toward the player no matter what? You have to drag the player object into the hierarchy where the script says "empty (Transform)" to make the enemy recognize the player.

avatar image ToxxicSin · Apr 10, 2014 at 12:42 AM 0
Share

From what I understand, you're losing health from an enemy being near when you actually just want the enemy to attack when it is near. Ins$$anonymous$$d of making the player lose health when the enemy is in range, make a timer of sorts, a fire rate for the enemy, so you're not constantly losing health.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Nequium · Apr 10, 2014 at 12:50 AM

The way your code is now, every frame that your enemy is close enough to your player, it is taking 1 health from your player. Games run at many frames per second, which is why your player's health is going down very quickly. You need to set some kind of timer variable to put time between attacks so that health doesn't drain all at once like that.

Define a float variable called "attackTimer" and set it to 0.0f, and change your code to this:

if(Vector3.Distance(transform.position,Player.position) <= MaxDist) {

if(attackTimer <= 0.0f) {

GameObject.FindGameObjectWithTag("Player").GetComponent(HealthBar).AdjustCurrentHealth(-1);

animation.Play("attack1");

attackTimer = 1.0f;

} else {

attackTimer -= 1.0f * Time.deltatime;

}

}`

That should do the trick. With this code, your enemy will only be able to attack once per second. You can change how quickly your enemy can attack by editing the 1.0f in the else statement. A bigger value will let him attack faster.

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
avatar image
0

Answer by getyour411 · Apr 10, 2014 at 12:12 AM

So the problem is that Update() runs many times per second, and for each of those frames where the distance check between enemy and player is less than MaxDist the player loses -1, and that adds up very fast (think "60 frames per second").

You should consider adding an attack delay, there are thousands of attackdelay timer examples already on UA/Google

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

24 People are following this question.

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

Related Questions

AI script troubles 1 Answer

How do I animate my enemy randomly with time? 1 Answer

2D enemy Field of Vision script 1 Answer

enemy ai going to player 2 Answers

C# AI, Enemy is flying at me when I jump. 2 Answers


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