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 chainylol · Oct 22, 2017 at 05:31 PM · collider2ddrag-and-drop

Drag and drop collisions

Hi, I have a problem with the OnTriggerEnter function. I have a few sprites in my scene with a drag and drop script attached to them. The script is taken from the internet and works fine. When a sprite is dragged and collides with a specified sprite, i want a function to be called. However, i only want it to work if the user isnt holding down the left mouse button, so that he has to "drop" the sprite for the collision to trigger.

Here is the code:

 void OnTriggerEnter2D (Collider2D col)
 {
     if (col.gameObject.name == "IconPlus" && !Input.GetMouseButtonDown (0)) {
         //transform.GetComponent<Renderer> ().material.color = Color.green;
         //Destroy(this.gameObject);
         //mainScript.inputNumber += ballnumber;
         Debug.Log ("Hit!");
     }
 }

If i remove the "&& !Input.GetMouseButtonDown(0)", the collision works fine. Please help.

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
1

Answer by tmalhassan · Oct 22, 2017 at 06:29 PM

Hello @chainylol. I guess what you're looking for is Input.GetMouseButtonUp(0). So what we do is check if the mouse is up and set the bool isBreakable to true. And once the object enters the trigger, it will register the hit. Of course, we will need the set the bool back to false so it would enable us to do it again. Except, there will be a drawback to this, because if the player clicks and releases anywhere else on the screen, it will set the bool to true. And once the object enters the trigger, it will register the hit. But luckily, you can work around this issue by creating another bool isHoldingObject to be set to true on OnMouseDrag() function and add the bool to the if statement condition in Update(). So the code would look like this:

 private bool isBreakable = false;
 private bool isHoldingObject = false;
 
 float distance = 10;
 int hitEnabler = 0;
 
 void Update()
 {
     if (Input.GetMouseButtonUp(0) && isHoldingObject == true)
     {
         hitEnabler = 0;
         isBreakable = true;
         isHoldingObject = false;
     }
 }
 
 void OnTriggerStay2D(Collider2D other)
 {
     if (hitEnabler == 0)
     {
         if (other.gameObject.name == "IconPlus" && isBreakable == true)
         {
             //transform.GetComponent<Renderer> ().material.color = Color.green;
             //Destroy(this.gameObject);
             //mainScript.inputNumber += ballnumber;
             Debug.Log("Hit!");
         }
     }
 
     hitEnabler = 1;
     isBreakable = false;
 }
 
 void OnMouseDrag()
 {
     isBreakable = false;
     isHoldingObject = true;
 
     Vector3 mousePosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance);
     Vector3 objPosition = Camera.main.ScreenToWorldPoint(mousePosition);
 
     transform.position = objPosition;
 }

Take note that this script has to be attached to the object that you're dragging. Also make sure that the object has a Rigidbody 2D component.

Hope this gives you a general idea on how to work around your issue, and all the best :)

Comment
Add comment · Show 18 · 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 chainylol · Oct 22, 2017 at 07:02 PM 0
Share

Hey man, thanks for the answer. I think you might be right about Input.Get$$anonymous$$ousebutton having to be used in the Update(), so i made a bool called "isBreakable"t hat turned false if the mouse button is held down inside the update() function.

I then added "&& isBreakable" to the if statement. It still doesnt work if i drag and drop the sprite like i want to, but if i place the sprite above the collider and let it fall down on it, the hit will register. Super weird.

avatar image tmalhassan chainylol · Oct 22, 2017 at 08:02 PM 0
Share

No, it won't work that way either. But you're close to getting the idea. I just updated my answer, take a look at it :)

avatar image chainylol · Oct 22, 2017 at 08:21 PM 0
Share

@tmalhassan Hmm, i copy/pasted your script, but it still doesnt work. Really dont know why...

avatar image tmalhassan chainylol · Oct 22, 2017 at 08:25 PM 0
Share

Weird, it worked for me. Did you make sure that On$$anonymous$$ouseDown() function is in a script attached to the object that you're dragging? I also forgot to mention that you need a collider on the object that you're dragging, and that collider must not be set to trigger.

avatar image chainylol tmalhassan · Oct 22, 2017 at 08:38 PM 0
Share

one object (the one im dragging) is a 2d sprite and the other one is a UI image. Both have circle colliders 2D set to trigger. The collision works fine without the Input.Get$$anonymous$$ouseButton part. I added the On$$anonymous$$ouseDown() function in the same script, and attached it to the object im dragging.

After the first collision has happened, it doesnt even matter if i hold down L$$anonymous$$B or not, It triggers anyway.

Show more comments

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

72 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 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 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 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 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

OnMouse Events for Composite Collider with Rigidbody2d 1 Answer

Drag and drop 3d game 1 Answer

Dragging an object in iOS (UnityScript) 3 Answers

how can i put just one child in a drag and drop container in ngui 1 Answer

How to create Arrows Between Two objects and Animate them 2 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