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 MegaStevenLP · Feb 27, 2013 at 05:45 PM · enemyhorrorhealth

Hurt Player if close to Enemy! Need Help

Im creating a little Horror Game in Unity! I donw all Basics (First Person Controller, Flashlight, World) and a Player-Following Enemy! Ive done the Scipt, that the Enemy follows the Player.

But how can i do, when the Player comes into Enemys Capsule Collider, that he loses Health?

IM NOT TRING TO RE CREATE SLENDER! THIS IS MY OWN IDEA!

Thanks

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

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Kiloblargh · Feb 27, 2013 at 06:15 PM

OnTriggerEnter()

collider.tag

if

==

-=

Have you read at any of the tutorials, or looked at the scripts on the built-in Angry Bots demo game?

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 Nightmare · Feb 27, 2013 at 08:16 PM

You should check the Angry Bot scripts.

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 KoshX87 · Feb 27, 2013 at 07:19 PM

Ok. You will need to make a sphere or a capsule or whatever collision zone you need (eg. if you want to make the enemies hurt you from a distance of 5 units, you make a 5 unit radius sphere.) Note: If you want the collision area to be normal, just use normal OnCollisionEnter. Using OnCollisionEnter() is straight forward and similar to what i will explain but, the if you use the capsule collider, the enemy be able to go through it if you make it bigger than your actual player.

So you make an empty Game object from the top menu and dag this onto your player object. Add a sphere collider of whatever radius you need to this new GameObject and rename the object to something intrinsic or obvious. Then select isTrigger and make a new script which will be placed on this GameObejct. If you haven't already look at the OnTriggerEnter(other:Collider) Function on Unity Scripting Reference.(Note: "other" can be renamed if you want but then you have to use that new name instead i.e destroy(whateverName.gameObject))

Once you have done that:

  • At the start of the code make a new variable of type GameObject (e.g. var self:gameObject). You will end up dragging that gameobject with the trigger on it.

  • if (other.gameObject.tag == "Zombie"){ playerHealth -= 20; //decrease playerHealth var by 20 if zombie enters the trigger area. }

  • So all you have to do is put your enemy's tag inside the quotation marks and if you have changed other at the begining of the function, use the new variable instead of other in the code i put above.

  • That should just about do it, but if you don't understand still please say the new or existing problem and what you want it to do instead

Comment
Add comment · Show 4 · 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 MegaStevenLP · Feb 27, 2013 at 08:16 PM 0
Share

this is my code

 if(Vector3.Distance(transform.position, thePlayer.position) <= 5) 
  
           {
               health -= damage * Time.deltaTime;
 }
 
   
 
 }
 
 
 if ( health == 0.0 )
     {
        health == 0.0;
        Application.LoadLevel( "gameover" );
     }


Now the Problem is, if the Player Health is 0, he doenst loads the Game Over Level, he counts to negative (-1, -2, ...)

avatar image Kiloblargh · Feb 27, 2013 at 11:07 PM 0
Share

I think what you meant to say is "if (health

avatar image MegaStevenLP · Feb 28, 2013 at 01:24 PM 0
Share

still doesnt works

 if (health <= 0.0)   {
        health == 0.0;
        Application.LoadLevel( "gameover" );
     }
     
  
avatar image KoshX87 · Mar 03, 2013 at 08:59 PM 0
Share

go to you're build settings and make sure the checkbox next to the gameover scene is ticked. It says in the script reference for Application.LoadLevel right here

avatar image
0

Answer by MegaStevenLP · Feb 27, 2013 at 10:44 PM

Created my own Script, thanks

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 Kiloblargh · Feb 27, 2013 at 11:10 PM 0
Share

Don't put comments as answers. If you solved the problem yourself, either delete or close the question, or post your script that you came up with and accept your own answer.

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

11 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

Related Questions

Change Health of a duplicated enemeis 1 Answer

Health to come above enemy when clicked 1 Answer

Problem with 2 scripts communicating 0 Answers

Taking a hit 3 Answers

How to create Enemy Health Bar ? 4 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