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 stay.metal · Feb 03, 2015 at 05:18 AM · collisionifdetect

Detect collision if

What im trying to accomplish is: When I click my mouse I want to detect if two colliders in my game are touching and if they are i want it to do something. what am i doing wrong?

 using UnityEngine;
 using System.Collections;
 
 public class Thetrigger : MonoBehaviour {
 
     public GameObject MyCollider;
 
     void OnCollisionStay(Collision collisionInfo){
         if (Input.GetMouseButtonDown (0)){
             if (collisionInfo.gameObject.name == "MyCollider") 
                 Debug.Log ("it works");}
         }
     }

Thank you

Comment
Add comment · Show 2
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 sethuraj · Feb 03, 2015 at 05:22 AM 0
Share

$$anonymous$$ake sure both objects have colliders and if one of the object should have a non kinematic rigid body attached.

avatar image stay.metal · Feb 03, 2015 at 05:31 AM 0
Share

they both have colliders and one have a rigidbody still doesnt work

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by hav_ngs_ru · Feb 03, 2015 at 10:05 AM

collision is fery fast thing, it could be that you dont have a time to click mouse while colliders are touching..

Try use Input.GetMouseButton instead - it works not only when you click but also when you hold button - it makes yuo more easy to catch a moment (just press it right before colliders should touch).

And to avoid multiple-events, make some Lock flag (set it true in OnMouseButtonDown, and set to false in OnMouseButtonUp and in "mouse pressed && OnCollisionEnter" event was handler.

PS Or you could make it vice-versa: continous collision (fire flag in OnCollisionEnter + run "hold-collision" timer in OnCollisionExit, like Invoke("FunctionThatSetsollisionFlagToFalse", 1f), and simple instamt mouse event, like this if(Input.GetMouseButtonDown() && collisionFlag) {...}

in this case player should press mouse at the collision moment or a little later, that is more intuitiveю

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 stay.metal · Feb 10, 2015 at 06:22 AM 0
Share

what do you mean by Lock Flag? or the term flag in general?

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

How to check if an object is in the players view? 1 Answer

Can't change variables on collision, if statement being ignored 1 Answer

Enemy Detection Problem in Pacxon Similar Game 0 Answers

If statement null check not working as expected 1 Answer

collider ignore other collider at a small angle 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