I want to drag a GameObject with the mouse but I need collision. The basic mouse dragging code simply sets the position of the object to the hit.point with an offset from the difference at the start of the drag.

As for collision, I've tried using kinematic rigidbodies and setting the position with rigidbody.MovePosition(uh) but that allows the object to rotate while pushing it up against a corner on one side. With rotation constraints enabled, it flickers a lot and still intersects a lot. I also tried playing with collision detection and interpolation. None of the settings improved it.

I also tried Physics.ComputePenetration but that is extremely messy because some of the objects I'm dragging, have several colliders. The distance it calculates is per collider so it doesn't calculate the offset required to separate the entire object. I tried disabling all the colliders and then adding a single box collider sized to fit the bounds of the object. That had bad flickering problems.

The last thing I tried was box casts on each side that would find the closest point and offset the mesh in that direction. But if I moved the mouse too fast it would go inside the other mesh and would no longer register any colliders intersecting the box casts. It also suffered from flickering problems.

Ideally, I want to have fairly accurate detection. I don't want to approximate the other colliders (not the ones on the object I'm dragging). Assume the other colliders could be any type (box/capsule/sphere/mesh).

The image below shows the problem I'm having. The green dot shows approximately where the cursor was (also the hit.point from the raycast on the surface of the red cube). What I expect is that the couch should be offset so that it was close to the cursor but prioritizes not intersecting other meshes over sticking to the mouse.

enter image description here

  • \$\begingroup\$ Have you tried moving the object with velocity instead of MovePosition? \$\endgroup\$ – DMGregory Feb 1 '19 at 12:23
  • \$\begingroup\$ I posted over here too: reddit.com/r/Unity3D/comments/alzby1/… Someone there also suggested velocity. I tried that and it yielded better results but now it gets stuck when you move the mouse far behind the obstacle - the rigidbody won't let it go around. \$\endgroup\$ – mr-matt Feb 1 '19 at 20:40
  • \$\begingroup\$ For going around an obstacle, you could try a strategy like this one \$\endgroup\$ – DMGregory Feb 1 '19 at 20:49
  • \$\begingroup\$ That looks very promising. Do you think this could work with boxcasts instead of spheres? My objects are not balls. \$\endgroup\$ – mr-matt Feb 1 '19 at 21:02
  • \$\begingroup\$ You'll need to handle some additional cases/state there, since you may have to slide "away" from your destination for several frames before a new opportunity to move "toward" (without backtracking) appears. The principles remain similar though. \$\endgroup\$ – DMGregory Feb 1 '19 at 21:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.