Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 kubawich · Apr 24, 2016 at 08:02 AM · unity 5scripting problemcollision

How to count from 2nd collision

Hi, i want to kill my player if it touch floor after landing on it, but problem is, that it's spawning on this floor, then jump, and then land, and after launching the game player is immediately killed. I'm using OnCollisionEnter, and I want to count to 2nd collision and then execute any action. How can I do this?

Comment
Add comment · Show 1
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 RiQQ92 · Apr 24, 2016 at 11:52 AM 0
Share

I'm not sure if this is the case anymore, but i had problem still in unity 4.6 when i had two colliders on same object, when other collided with something it called collision from both colliders, which in your case could result immediate increment x colliders. This happened with 2D Physics.

@kubawich

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by MrAkroMenToS · Apr 24, 2016 at 10:04 AM

Hi @kubawich

Declare a counter somewhere and increase it in the OnCollisionEnter block when you collide with the desired object. Upon collision add +1 to the counter, and when it reaches 2 then perform the action.

It should look something like this:

 int counter = 0;
 void OnCollisionEnter(Collision col) {
     counter = counter + 1;
     if (counter == 2) {
         // Do your stuff
     }
 }

Or you can just spawn the player outside of the collider.

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 kubawich · Apr 24, 2016 at 10:22 AM 0
Share

Acctually i tried this before, but problem is that OnCollisionEnter's working like loop, and when i spawn my player on floor the counter is incrementing through infinity, and destroy plyer object as fast as i start the game. Here's code for this http://pastebin.com/1NPc4XAg I used IsCollIncremented to not kill player immiedetly but idk how can I increment this just by one, not infinity :v

avatar image MrAkroMenToS · Apr 24, 2016 at 10:34 AM 0
Share

OnCollisionEnter sould work only once, upon entering the other collider, a dirty fix would be to say something like this:

  void OnCollisionEnter(Collision col) {
     if(Time.time > 1f){  // You can change '1f' to whatever you want, it is in seconds. It will be true when the game is running for at least 1 second, so basically you give the player a 1 sec long immunityFrame
         // Do your stuff    
     }

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

Mesh deformation of a high poly 3D model causes FPS dropping 0 Answers

How can i look for collisions of the bulidngs in may array ? how can i use OnTRiggerEnter/exit ? 0 Answers

Attaching a script at runtime 2 Answers

How can I remove an object when colliding with an other object? 1 Answer

Issues on spawning the enemy? 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