I have an object that I use to catch other objects. I can catch the objects and still move the catcher with velocity with OnMouseDown, but when I use the following MouseDrag script, the caught objects fall through the catcher. Any help would be greatly appreciated.
public class MouseDrag : MonoBehaviour {
Vector3 dist;
float x;
float y;
private void OnMouseDown()
{
dist = Camera.main.WorldToScreenPoint(transform.position);
x = Input.mousePosition.x - dist.x;
y = Input.mousePosition.y - dist.y;
}
void OnMouseDrag()
{
Vector3 curPos = new Vector3(Input.mousePosition.x - x, Input.mousePosition.y - y, dist.z);
Vector3 worldPos = Camera.main.ScreenToWorldPoint(curPos);
Also, when simulating physics, you can’t just update the transform position. You must move it via the Rigidbody interface by either applying a force w/ AddForce, or moving via MovePosition.
And this must be done in the FixedUpdate.
So… have Rigidbody reference in your script, as well as Vector3 to store target position.
In Mouse events update target position with new desired position.
In FixedUpdate tell the Rigidbody to move to that target position.