Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 sutterman21 · Jul 13, 2015 at 01:18 AM · collisionplayerdeath

OnCollisionEnter issues

I cannot for the life of me figure out what I am doing wrong. I have gone through several different tutorials and walkthroughs, but nothing seems to work. I want a "Ceiling" to eventually crush the "Player" if they don't make it to the exit in time. The player is tagged as "Player" and has a kinematic rigidbody attached. The ceiling has the following script attached.

  function OnCollisionEnter (collision : Collision) {
      
      if(collision.gameObject.tag == "Player"){
      
          Destroy(gameObject);
          
  }
  
  }

Can anyone help me out with this? I know it has to be something simple...

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Aridez · Jul 13, 2015 at 01:37 AM

I understand that you want to destroy the player object and you said that "The ceiling has the following script attached", so when you do "Destroy(gameObject);" you are destroying the ceiling (if the code works). Change it to "Destroy(collision.gameObject);" and that should work, let me know if that solves the issue!

Comment
Add comment · Show 19 · 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 sutterman21 · Jul 14, 2015 at 07:22 PM 0
Share

I'm not sure what I'm doing wrong, but everything I've tried just isn't working. :( It seems like no matter what I do, the ceiling just passes right through my character...

avatar image Aridez · Jul 14, 2015 at 08:29 PM 0
Share

Did you try the changes I posted above? If the ceiling is passing trhough your character maybe something is wrong with the set up of colliders (they should at least collide between them), how did you set up the scene?

avatar image sutterman21 · Jul 14, 2015 at 09:23 PM 0
Share

I made the changes and it didn't effect anything :/ Here is the Inspector panel for my player and the ceiling, maybe you can see something that I can't... (Also, I do 'collide' with the ceiling. It stops my player, but it just isn't killing the player..)

layout.jpg (80.7 kB)
avatar image nadhimali · Jul 14, 2015 at 09:25 PM 0
Share

are you sure that the ceiling layer is allowed to collide with the player layer from Edit>Project settings>Physics>matrix ? From your Picture both objects are in the same layer, however the layer can be set not to collide with itself.

avatar image Aridez · Jul 14, 2015 at 10:04 PM 1
Share

I found out what could it be here: "You can move a kinematic rigidbody object from a script by modifying its Transform Component but it will not respond to collisions and forces like a non-kinematic rigidbody."

Looking at the chart on the "collision action matrix" at the bottom of the page linked we can see how collisions between "$$anonymous$$inematic Rigidbody Collider" and "Static Collider" are not detected.

You can either make the rigidbody you have non kinematic (unchecking it in the inspector) or you can add a rigidbody to the ceiling.

Show more comments
avatar image
1

Answer by nadhimali · Jul 13, 2015 at 11:00 AM

This code will destroy the ceiling and also your code. you found the player by tag but the destroy action will not be applied to that player unless you correctly specify it.

   function OnCollisionEnter (collision : Collision) {
       
       if(collision.gameObject.tag == "Player"){
       
           Destroy(collision.gameObject);
           
   }
   
   }
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 TruffelsAndOranges · Jul 14, 2015 at 10:48 PM

Destroy does NOT destroy the gameObject immedietly, it just tells the engine to destroy the game object whenever the engine wants to!

Try this instead:

 gameObject.SetActive(false);
 Destroy(gameObject);


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.

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

Cant Find Destruction Code? 1 Answer

I want my player to die when collide with enemy from front or back and not when it jumps on it (i alread tried checking if player is grounded or not) and if someone could provide script for it ill be glad 0 Answers

How to prevent losing multiple lives at zero health? 2 Answers

Help with player death on collision! 2 Answers

How to detect player collision 0 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