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 FizzyBear · Apr 16, 2012 at 01:46 PM · animationattackmeleehealth

Not identifying the collider?

Basically, in reference to my healthbar (which works perfectly fine), I have my enemy running up to me and attacking when (distance<1), however it's not detecting any colliders at all, they all have character controller/rigidbody etc. It just doesn't remove health upon impact. How do I do this?

This is my code.

 @script ExecuteInEditMode()
 var healthTexture : Texture2D;
 var healthBorder : Texture2D;
 
 enemy = GameObject.Find("Enemy");
 player = GameObject.Find("Player");
 
 var health : int = 100;
 var adjust : int = 0;
 
 function OnCollision(other:Collision)
 {
     Debug.Log("Collided with " + other.gameObject.name);
     if (other.gameObject.tag == "Enemy")
     {
         other.gameObject.tag == "Player";
             health -= 10;
     }
 }
 
 function OnGUI () {
     
     GUI.DrawTexture(Rect(0,Screen.height - 600,290,100), healthBorder);
 
     //Now on top we can put Health Bar texture
     adjust = health * 1.5;                       //adjusting texture size (width) /       health(100)
     GUI.BeginGroup(Rect(122,Screen.height - 565,adjust,15));
     GUI.DrawTexture(Rect(0,0,290,15), healthTexture);
     GUI.EndGroup();
 
 }

Look forward to your answers.

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
1
Best Answer

Answer by JinxM · Apr 16, 2012 at 02:14 PM

Likely the error is that you created a custom function OnCollision() instead of using the event OnCollisionEnter().

Also your if statement looks a bit strange to me. The other.gameObject.tag == "Player" line doesn't do anything.

Is this script placed on your Player object? If so, then I'd expect to see something more like this:

 function OnCollisionEnter (other : Collision) {
     if (other.gameObject.CompareTag ("Enemy")) {
         health -= 10;
     }
 }
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 FizzyBear · Apr 16, 2012 at 03:38 PM 0
Share

Sorry, I had my code like this before I changed it, it was the same scenario. 0 change :/

avatar image
1

Answer by aldonaletto · Apr 16, 2012 at 02:15 PM

Collisions may be tricky: OnCollision events are sent to the scripts attached to both objects when the rigidbody hits the CharacterController, but not when the character hits the rigidbody. To detect the character hitting something, you must use OnControllerColliderHit in the character script (nothing is sent to the other object).
In order to apply damage to the other object when a character hits it, you must use something like this (character script):

function OnControllerColliderHit(hit: ControllerColliderHit){
  if (hit.gameObject.tag == "Enemy"){
    // call the function ApplyDamage in the enemy script:
    hit.gameObject.SendMessage("ApplyDamage",10);
  }
}
And you must add the ApplyDamage function to the enemy script:

function ApplyDamage(damage: float){
  health -= damage;
  if (health 
The same applies to the enemy-hitting-player case.


                    
Comment
Add comment · Show 5 · 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 FizzyBear · Apr 16, 2012 at 03:38 PM 0
Share

This doesn't seem to work, it's not identifying the collider AT all.

avatar image aldonaletto · Apr 16, 2012 at 08:01 PM 0
Share

Both answers are right - $$anonymous$$e and @Jinx$$anonymous$$'s - one of them should work, depending on some details. What's the player? A character controller or a rigidbody? And what about the enemy?

avatar image FizzyBear · Apr 17, 2012 at 05:52 AM 0
Share

Both of them are character controllers.

I've even removed the character controller from the enemy and replaced it with a rigidbody.

I've even tried

 function OnControllerColliderHit(hit: ControllerColliderHit){
 print("Does this work yet?");
 }

But it doesn't print out anything. :/

UPDATE - Nvm, I got it working now. Just need to figure out how to make my enemy stop 2m from the player ¬_¬

avatar image bodec · Apr 17, 2012 at 03:03 PM 0
Share

almost sounds like one of the charaters his "players"is not tagged player. what was the problem if you don't $$anonymous$$d sharing. curosity so that if i run into this problem i know where to look.

avatar image FizzyBear · Apr 17, 2012 at 04:40 PM 0
Share

The sword itself didn't have a collider, so I set a box collider on the sword and did a print out. Everyone has a tag, as when I played the animation, the mesh collider on the sword was duplicated and pushed to another area. (Spartan$$anonymous$$ing model from the asset store, load it and you'll see what I mean).

That was about it.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

Fighting combo attacks 1 Answer

Melee Damage script by collision 2 Answers

Animations Combo 0 Answers

Changing specific animation of a person 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