- Home /
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.
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
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?
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.
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.
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.
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!