• 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 Wintergrasped · Jan 12, 2014 at 03:53 AM · javascriptenemyhealth

Apply Damage To Player On Collison With Specific Game Object

Yes I do realize this question has been asked many time before. but i have never found one that fits my needs, and yes i am new and i am taking this project 1 step at a time

  1. Enemy Object name = enemy

  2. player is Main Camera

  3. Health Script

    var Health = 100;

    function ApplyDammage (TheDammage : int) { Health -= TheDammage;

      if(Health <= 0)
         {
             Dead();
         }
     }
     
     function Dead()
     {
         Destroy (gameObject);
     }
    
    
  4. Raycast Shooting Script

    var Effect : Transform; var TheDammage = 100;

    function Update () {

       var hit : RaycastHit;
         var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5, Screen.height*0.5, 0));
         
         if (Input.GetMouseButtonDown(0))
         {
             if (Physics.Raycast (ray, hit, 100))
             {
                 var particleClone = Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
                 Destroy(particleClone.gameObject, 2);
                 hit.transform.SendMessage("ApplyDammage", TheDammage, SendMessageOptions.DontRequireReceiver);
             }
         }
         
     }
    
    
  5. Current attempt that dont work var health = 4; var damage = 1; var wait_time = 2; var lock = 0;

       function OnCollisionEnter(hit: Collision){
         if(hit.gameObject.tag == "enemy" && lock == 0)
                 {
                 hit.transform.SendMessage("ApplyDammage", 100, SendMessageOptions.DontRequireReceiver);
                 }
                 }
    
Comment
Add comment · Show 2
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 getyour411 · Jan 12, 2014 at 04:07 AM 0
Share

Add some debug to your OnCollision to see if you ever get what you expect. If #5 is attached to Player, do you have a collider (not a character controller) or rigidbody?

avatar image Wintergrasped · Jan 12, 2014 at 06:04 AM 0
Share

Yea it has a capsule collider

1 Reply

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

Answer by Seth-Bergman · Jan 12, 2014 at 07:48 AM

tag and name are two different things.. make sure you create a TAG called enemy and give your object that tag.. Or change your code to:

 if(hit.gameObject.name == "enemy" && lock == 0)    

also, OnCollision functions may require a Rigidbody to work.. there is a matrix toward the bottom of this link to show what will send a collision message and what won't:

http://docs.unity3d.com/Documentation/Components/class-BoxCollider.html

you may be better off using a trigger. just set the collider on the object to "isTrigger" and change your "OnCollisionEnter" to "OnTriggerEnter".. You will still need a Rigidbody (or CharacterController), either way.. (And for the record, there are LOTS of examples out there which explain this, you probably should look harder..)

Finally, if this or any answer helps you, don't forget to accept it by clicking the checkmark.. Welcome to the forum!

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.

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

19 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

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

How Do I Make A Health Bar 6 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

how can i do a headshot with a rigidbody bullet? 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