i have a moving gameobject in unity 3d. Gameobject is rigid body with colider.
if i swipe that object slowly, i can detect object by using raycast using code
Couple of things: (edited to reflect Benproductions input). If you use an OnGui function, not Update(), it will send a mousedrag event, but this doesn’t update every pixel, whether it runs faster than update() I’m now unsure. it’s limited by the interrupt speed by a computer I believe. So one solution is to draw a line between each mouse drag event, then test maybe 5 ray casts at equal intervals along the line and see if you get a hit (the first point will be the touch point, and don’t do any ray casts if the touch points are not on either side of the object (you can test by remember the last drag point raycast, and casting a ray at the point of the mousedrag event.
I’m assuming you are drawing a 2d line over a 3d object. If it’s a 2d object with 2d line performance is even better, fit a rectangle around the object and use Rect.Contains() (it uses AABB).
This may not be applicable, but more efficient: if you instead get the first contact point, and last contact point, it’s easy to tell if your line passed through the object, and you only have to detect on touch and on release rather than look several times a frame. If you combine this with translating the 3d object’s bounding box into 2d screen space, then you don’t need to go about casting tons of rays at different points in the line, you can just check the few verts of the screen space box and stop as soon as you see one vert above the line and one below it. You can still draw your line normally, but don’t test collision until you’re done touching…Not sure if that will work with your game dynamics.
Move you object with basic input, you may have to create you own drag method and then store the object position and draw a linecast between current and previous. If the line is cut it will return false.