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 Grady · Dec 15, 2011 at 10:31 AM · collisionplayerfpsenemy

Taking damage from an enemy on collision

Hey guys,

What I have done so far is created a first person shooter where you shoot enemies that walk around and follow you. I've got everything working fine. I can shoot the enemies three times each, and they die etc.... etc...

What I am stuck on, is getting the player to take damage when enemies collide or come into contact with the player. I tried the following script:

 var health = 4;
 
     function OnCollisionEnter(hit: Collision){
         if(hit.gameObject.tag == "enemy"){
             health = health - 1;
         }
     }
     
     function Update(){
         if(health == 0){
             Destroy(gameObject);
         }
     }

This is the script to go onto the player and as you can notice, I ran into a problem, because when the enemies collide with the player, it is constantly taking down the health, and because the Update function is constantly checking how ever many times it does per frame, it basically kills the player straight away....

What I was thinking was, I need say a cool down buffer, where it takes a couple of seconds until the player can take damage again, even if an enemy is colliding with it!!!

Thanks in advance!

-Grady

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

2 Replies

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

Answer by BiG · Dec 15, 2011 at 10:42 AM

This is a very basic solution, but maybe is what you want. The only change is the introduction of a WaitForSeconds() that stops the health decreasing for 2 secs. I can't test it right now, so it's just a suggestion. Let me know...

 var health = 4;
 var damage = 1;
 var wait_time = 2;
 var lock = 0;
 
 
     function OnCollisionEnter(hit: Collision){
         if(hit.gameObject.tag == "enemy" && lock == 0){
            DecreasingHealth();
         }
     }
 
     function DecreasingHealth(){
         lock=1;
         health=health-damage;
         yield WaitForSeconds(wait_time);
         lock=0;
     }
 
     function Update(){
        if(health == 0){
           Destroy(gameObject);
        }
     }
Comment
Add comment · Show 6 · 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 Grady · Dec 15, 2011 at 10:44 PM 1
Share

Ok thank you, I'm not at unity right now, but I will test it after! Thanks for your response!

avatar image Grady · Dec 17, 2011 at 02:28 AM 0
Share

for some reason, the collision is not be detected, i have a character controller on my first person player and i attached a non kinematic rigidbody with use gravity disabled.

I don't know why this isn't working.....

avatar image BiG · Dec 19, 2011 at 07:18 AM 0
Share

Is isTrigger deactivated under the Collider voice? 'Cause all seems working to me...

avatar image Grady · Dec 19, 2011 at 08:31 AM 0
Share

yeah, is trigger is deactivated on the enemy, but i can't check the players one because it is a first person controller with a character controller....

avatar image DeathByWarrior · Dec 29, 2011 at 01:10 PM 2
Share

$$anonymous$$aybe it won't work because character controllers don't use OnCollisionEnter? I think they use OnControllerColliderHit.

Show more comments
avatar image
0

Answer by elpedro_75 · Apr 17, 2014 at 12:51 PM

I would try the following approach:

  1. Create a flag (Boolean) to toggle recognition of the collision, "hasCollided".

  2. Inside OnCollisionEnter, first check that hasCollided is false. If it is, flag it as true and calculate the damage. This should prevent subsequent calculations of the same collision.

  3. Finally, use OnCollisionExit to reset hasCollided to false. This should ensure that the objects have to separate before a subsequent collision is recognised.

I hope that solves your problem. (I'm going to be facing the same thing shortly...)

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

When I Destroy 1 Enemy They all Get Destroyed. PLEASE HELP 2 Answers

Decreasing Player Health On Collision with Enemy 2 Answers

Enemy Collision Wont Work! PLEASE HELP 0 Answers

Player Dies when Collide with enemy 2 Answers

Only targeting the targets root of vector 3 and not colliding with its mesh 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