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
1
Question by franco-carlos25 · May 18, 2013 at 10:10 PM · charactercontrollercollision detectionontriggerenterbulletoncontrollercolliderhit

Enemy with character controller has inconsistent hit detection

I've created an enemy and attached a character controller to it. In the past, it had a simple capsule collider set as a trigger, to be used to detect player bullets. All was fine until I started using character controllers instead so that enemies don't pass through obstacles. Now that I've changed it, the bullets will register seemingly only half of the time, and even then the bullets are not being destroyed as they should. The other half of the time they will just pass through without causing any damage.

This is the code on the enemy detailing the collision detection for player bullets:

 function OnTriggerEnter (other : Collider)
     {
         if (other.gameObject.tag == "Bullet")
         {
             var bullet1 : BulletScript = other.GetComponent(BulletScript);
             health -= bullet1.damage;
         }
     }

And this is the bullet's code detailing enemy collision:

 var damage : int;

 function OnTriggerEnter (other : Collider)
 {
     if (other.gameObject.tag == "Environment")
     {
         impact.PlayClipAtPoint(clank, transform.position);
         Destroy(gameObject);
     }
     else if (other.gameObject.tag == "Monster")
     {
         impact.PlayClipAtPoint(hit, transform.position);
         Destroy(gameObject);
     }
 }

The bullets have a box collider set as a trigger and a rigid body.

I probably should not be using OnTriggerEnter on the enemy, but nothing else has worked. Not OnCollisionEnter, not OnControllerColliderHit (the game thinks the bullets are Untagged when I do this, even though they're not). Changing the bullet's function to OnControllerColliderHit has the same effect as OnTriggerEnter. I've tried adding an extra capsule collider on the enemy, but that only causes hits to register twice. So unless I've been using these functions incorrectly, I don't know how to fix this.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by aldonaletto · May 18, 2013 at 10:40 PM

Usually bullets are rigidbodies and use OnCollisionEnter to detect when something is hit (bullet script):

 var damage : int;
  
 function OnCollisionEnter (col : Collision)
 {
     if (col.gameObject.tag == "Environment")
     {
        impact.PlayClipAtPoint(clank, transform.position);
        Destroy(gameObject);
     }
     else if (col.gameObject.tag == "Monster")
     {
        impact.PlayClipAtPoint(hit, transform.position);
        Destroy(gameObject);
        // try to get the enemy script EnemyHealth:
        var eScript: EnemyHealth = col.gameObject.GetComponent(EnemyHealth);
        // if found, decrement the enemy health
        if (eScript) eScript.health -= damage;
     }
 }

The enemy script don't even need to know that it was hit - the bullet script itself can do the job like above.

Maybe your problem is being caused by the discrete nature of collision detection: when a projectile moves reasonably fast, it may be before the target in one frame and after it in the next, and no collisions are detected. A simple solution is to elongate the bullet collider a lot, so that it extends way ahead the bullet:

alt text

Since the physics default rate is 50 cycles per second, a collider with about 0.5 meter long can travel at up 0.5 * 50 = 25 meters per second without miss any hit - smaller colliders at this speed will depend on the target width in order to not miss the collision.


extendedcollider.png (1.0 kB)
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 riffraffgames · May 19, 2013 at 02:11 AM 0
Share

Excellent answer aldonaletto! I was recently struggling with collision between enemies (who were using the Character Controller but not always moving) and projectiles trying to kill them. Was simple to use OnTriggerEnter when one of them was a trigger. I noticed that whichever object had the "Is Trigger" checked would not do any kind of collision with other static rigidbodies.

Using OnCollisionEnter as you've described above looks to have solved all the issues ;)

avatar image aldonaletto · May 21, 2013 at 01:37 AM 0
Share

Yes, static triggers can detect rigidbodies or CharacterControllers that hit them, but a moving trigger only works if it has a rigidbody (usually a kinematic one).

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

15 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

Related Questions

Could some one help me with this script... 5 Answers

OnControllerColliderHit Doesn't always get detected? 0 Answers

destroy a non-trigger object hitting a trigger object? 1 Answer

Damage Player when this object collides the CharacterController? 2 Answers

OnTriggerEnter for moving object 3 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