• 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 kiko · Jul 19, 2011 at 03:55 PM · javascript

damage once hits

hi guys i need help in damage script.i have a capsule object which is known as player, and a sphere object as enemy.how to i make the sphere object destroy itself when collide with the capsule and taking away one health points from the capsule.

here is my capsule collide script: var Lives = 1; var HealthControl;

 function OnTriggerEnter( hit : Collider )

{
if(hit.gameObject.tag == "fallout") { dead = true; //substract life here HealthControl.Lives -= 1;

             }
         if(hit.gameObject.tag == "enemy")
         {
                 gotHit = true;
                 HealthControl.Hits -= 1;
                 Destroy(hit.gameObject);
         Debug.Log ("HealthControl.lives -1!");
         }    
 }
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 Evil-Dog · Jul 20, 2011 at 03:11 PM 0
Share

make sure you vote the good answers and flag the final answer if there's one

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Kith · Jul 19, 2011 at 04:51 PM

http://answers.unity3d.com/questions/13784/players-projectile-communicating-with-enemy.html

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 Evil-Dog · Jul 19, 2011 at 04:12 PM

First, make sure your OnTriggerEnter is triggered by putting a print

 print("OnTriggerEnter:" + hit.gameObject.tag);
 

Then you'll know 2 things, if your triggers are setup correctly and if the tags are setup correctly. Your destroy seems fine and your health stuff you haven't explain how they work.

If your OnTriggerEnter is not triggered it can mean a couple things, make sure your enemy has a collider with the "IsTrigger" flag set to true and make sure your player has an actual physics component, not just a trigger.

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 Shealei · Jul 19, 2011 at 04:12 PM

I would say, first define your health or lives (what ever you are using) as a static variable on your player script(The scripts which is attached to your player). Lets say your scripts name is Player and your static variable name is HEALTH, ok now you can acces that variable with this script and reduce the amount by a desirable amount. Attach this to your sphere.


function OnControllerColliderHit(hit : ControllerColliderHit)

{

// Player is your player simply...

if(hit.gameObject.tag == "Player")

{

// this destroys the sphere and substracts -1 HEALTH from your Player script. You can print this health later by the next function.

 Destroy(hit.gameObject);
 
 Player.HEALTH --;

// This basically displays your new health amount on the screen. And there you go, wallah

 GameObject.Find("YOUR_HEALTH_DISPLAY_GUI_NAME").guiText.text = ""+Player.HEALTH;

}

}


So now, you can change the names of player and YOUR_HEALTH_DISPLAY_GUI_NAME things accordingly so that you have your desired results. In your script that contains the HEALTH static variable, the one on your player, you can do whatver you want with that later on. Like


if (HEALTH < 10) { DO SOMETHING }


Hope that helpled..

Comment
Add comment · Show 2 · 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 kiko · Jul 20, 2011 at 02:46 PM 0
Share

its working, thanks guys for all of your helps. There one more question i want to ask, how to add mesh filter in the object that i just import from c4d?

avatar image Shealei · Jul 20, 2011 at 09:52 PM 0
Share

Add it inside C4D ?!?

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make a N-S magnet Javascript? 1 Answer

How do I make a sword swing? 2 Answers

{Easy question} How to disable script from other gameObject. 2 Answers

How to display GUI in sequence in trigger 3 Answers

script linking 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