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 user-3061 (yahoo) · Dec 15, 2010 at 05:04 AM · collisionaidamagehealth

Ai that applies damage in collision?

Hi, I need o make a script that will make an enemy apply damage when it collides with the player, and when the player's health reaches 0, reload the level. Here's my simple script :

Player Script :

var health = 50;

function ApplyDamage (damage : int) { health -= damage;

  if(health <= 0) {
      Die();
  }

} function Die () { Application.LoadLevel (Application.loadedLevel); }

Enemy Script :

var damage = 10;

function OnCollisionEnter (col : Collision) {

col.gameObject.BroadCastMessage("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver); }

I think its a simple script, when i use it on some enemies, my FPS dropped like hell with about 10 enemies on my screen ( only happened while using scripts ), and doesn't damage my player however they collide... is there any problems in this script? Can anyone tell me whats the problem?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by zannghast · Dec 15, 2010 at 09:01 AM

What could be happening is that your enemy objects are colliding at a constant rate (a very fast rate, at that. With other colliders in the area, perhaps?).

If this is true (and I'm assuming it's doing so every frame), then those 10 enemy objects are broadcasting a message every frame.

If this were on a mobile device, you'd probably never even get the chance to load that particular level hehe.

What you could do is:

  • To check if your enemy objects are sending the message even if they don't need to, you could put a Debug.Log("Collided with: " + col.gameObject.name) inside the OnCOllisionEnter function.
  • Try putting a condition inside the OnCollisionEnter function so that the enemy object would only broadcast that message everytime it collides with a specific object (e.g. the player object). If you need help with that, just say so.
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 user-3061 (yahoo) · Dec 16, 2010 at 04:04 AM 0
Share

ok il try that, thanks for the reply..

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

No one has followed this question yet.

Related Questions

Player take damage on collision with AI 1 Answer

Editing a variable from another script on collision 3 Answers

Damage trigger? 1 Answer

Enemy Health Damage 2 Answers

Ai Aint Taking Damage From Bullet (FPS Rocket) 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