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 inman · Nov 10, 2014 at 06:29 PM · 2dcollisionmouseswipe

Detect mouse swipe on 2d object

Hey all,

I am trying to detect a mouse swipe using OverlapPoint on a 2d object that has a circle collider. I've spent a lot of time researching the new way to detect overlap points with 2d objects, and this code is working 80% of the time. In my start function for the spawned object I have:

 c_collider2d = gameObject.GetComponent<Collider2D>();

My update function looks like this:

 void Update() {

     if (!paused) 
     {
         if(Input.GetMouseButtonDown(0))
         {
             mouseIsDown = true;
         }

         if (mouseIsDown)
         {
             Vector3 wp = Camera.main.ScreenToWorldPoint(Input.mousePosition);
             Vector2 mousePos = new Vector2(wp.x, wp.y);

             if (c_collider2d == Physics2D.OverlapPoint(mousePos))
             {
                 Debug.Log ("collision *****"); 
                 Destroy (this.gameObject);
             }
         }

         if (Input.GetMouseButtonUp (0))
         {
             mouseIsDown = false;
         }

         transform.Rotate (Vector3.forward * rotationValue);
     }
 }

Obviously, I am missing something. 80% is great, but that isn't going to cut it.

Does anyone see what I might be missing? (I've tried this with both Collider2D and CircleCollider2d and there is no difference in the ability to detect the overlap point.)

Any assistance is appreciated. Many thanks in advance.

Comment
Add comment · Show 5
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 AlwaysSunny · Nov 10, 2014 at 05:11 PM 0
Share

I'd suggest using Get$$anonymous$$ouseButton() ins$$anonymous$$d of the up-down trick, for various reasons. Not that it's responsible for the issue. I don't see any "swipe" action here, just a check for whether the object is beneath the mouse when the button is down. Can you define what's happening when the check fails? In other words, what might be different on those occasions?

Also note this, from the docs: If more than one collider overlaps the point then the one returned will be the one with the lowest Z coordinate value. Null is returned if there are no colliders over the point

avatar image inman · Nov 10, 2014 at 05:19 PM 0
Share

Okay, I switched the flag so that it detects the state of the Get$$anonymous$$ouseButton rather than using my bool mouseIsDown. But I still have the same results. Sometimes it detects the collision, other times it doesn't. When the check fails, the 2d object falls and is destroyed after it goes off the screen. Then another 2d object is spawned and thrown on the screen for players to try to swipe (Fruit Ninja style).

I wonder if this has to do with the physics set up for the thrown object?

avatar image AlwaysSunny · Nov 10, 2014 at 05:22 PM 0
Share

Ideally, you should collect input during Update() and perform anything related to the physics system in FixedUpdate(), including OverlapPoint. Again, that might not be the source of the error, but it's a good practice to adopt to ensure physics stuff happens in tandem with the simulation.

avatar image inman · Nov 10, 2014 at 05:34 PM 0
Share

That makes sense. I modified my code so that input is gathered in Update and the physics are now checked within the FixedUpdate, but alas, the error is not resolved.

avatar image AlwaysSunny · Nov 10, 2014 at 06:31 PM 0
Share

Are you absolutely convinced there's an issue occurring? You're not, in other words, missing the object with the swipes? Note that if your mouse / finger moves too quickly, it may pass through the object completely. This is a very common physics misunderstanding we see every other day.

The correct way to detect a "swipe" is by creating a line between the current and one or more previous input positions and checking that line against your colliders.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by inman · Nov 10, 2014 at 08:56 PM

Yeah, I'm convinced. I'm swiping very carefully and the objects are larger and moving at a reasonable speed right now for proof of concept testing. I think it's a physics issue, as it works almost every time when the object is falling and my mouse is swiping up. Other swipes are not so accurate.

I actually started out with the line creation solution, since I had that already working in a 3d space. I then abandoned it as I kept reading about 2d objects--it seems like the other solutions would be a bit more elegant for the 2d world given some of the newer functions. I'll try one more fix with this, and then, if it doesn't work, I'll go back to my line creation code and see if I can successfully modify it for my sprites. Either way, I'ill post a solution once I have it working.

Thanks for your feedback!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How would I move an object towards the mouse but not stick to it. 1 Answer

2D Detect collisions of a 2D block only on left/right (not top/bottom) 0 Answers

Objects don't collide 1 Answer

Particles and 2D games 1 Answer

Apply force towards mouse click 2D C# 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