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 stingman · Jan 22, 2012 at 11:18 PM · collisioncolliderdestroyontriggerenteroncollisionenter

2 objects collide, need to destroy one

Hello, I have 2 objects with rigidbodies attached. One is a character controller (the character I'm moving) and the other is an enemy NPC. When my character collides with this enemy NPC I want him to destroy the NPC. I couldn't figure out how to do it with OnCollisionEnter so I am trying OnTriggerEnter and I created a capsule collider, attached it to the enemy NPC with "is trigger" checked. Any ideas? Everything I read shows this should be pretty simple but I can't seem to figure it out. The following script is attached to the enemy NPC:

function OnTriggerEnter(hit : Collider){

if (hit.gameObject.tag == "Player2") { Destroy (gameObject); } }

Comment
Add comment · Show 4
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 syclamoth · Jan 22, 2012 at 11:30 PM 0
Share

Can you tell us more? For example, is your character actually being moved by the rigidbody, or is it just slaved to a CharacterController component? What you're trying to do should work, so presumably there's something else happening here.

avatar image stingman · Jan 22, 2012 at 11:47 PM 0
Share

the character is being moved by a character controller component.

avatar image syclamoth · Jan 22, 2012 at 11:49 PM 0
Share

Well, that's the problem. CharacterControllers hate physics, and ignore it whenever they can.

avatar image stingman · Jan 23, 2012 at 12:25 AM 0
Share

so i basically need to remove the character controller, put on a sphere collider and re-write all of my scripting for my character's movement? There has got to be a better way...

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by IronFurball · Jan 22, 2012 at 11:36 PM

if im right(not sure) there are a couple of things wrong,or maybe u didnt tell us yet :P

the player(the object that moves) needs to have both a rigidbody and a collider and i think the script needs to be attached to him instead of the npc, try that and let us know if it works.

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
1

Answer by aldonaletto · Jan 22, 2012 at 11:44 PM

If the capsule collider is a component added to the NPC, it should work, but if it's an object childed to the NPC, only the trigger will be destroyed (you should use Destroy(transform.parent.gameObject) in this case).
Another point: the collision character->trigger works fine, but trigger->character is detected only if the trigger has a rigidbody (even a kinematic one): if the trigger is childed to the NPC, make sure it has a kinematic rigidbody (a non-kinematic rigidbody may be moved or rotated by AddForce or AddTorque).

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 stingman · Jan 22, 2012 at 11:53 PM 0
Share

i'm going to need to mess around with this more... the npc moves through waypoints so I'm not sure if this is affecting something. The capsule collider (trigger) is a component added to the NPC, not an object childed to the NPC.

avatar image aldonaletto · Jan 23, 2012 at 01:31 PM 1
Share

if the NPC is a CharacterController, move it with $$anonymous$$ove or Simple$$anonymous$$ove - just modifying its position directly or via Translate may frustrate the collision detection.

avatar image Bunny83 · Jan 23, 2012 at 01:58 PM 1
Share

Just like to add that CharacterControllers have their own collision method: see CharacterController.OnControllerColliderHit

avatar image stingman · Jan 23, 2012 at 05:24 PM 0
Share

thank you for the tip... I figured that part out as well. I got it working finally!

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

8 People are following this question.

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

Related Questions

OnTriggerEnter() and OnCollisionEnter(), OnControllerColliderHit() not working with character controller 2 Answers

Destroy object 1 if player walks into object 2 1 Answer

Colliding two GameObjects 1 Answer

Keep doing something while colliding 1 Answer

How to make gameObject make only one collision 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