Hello,
I’ve got 2 objects - a sphere and a cube. When I drag the sphere and hit the cube with it, the cube is flying away. How can I make the hit objects stay in the same position even if they are hit with the another (dragged) object? When I checked the Constraints-Freeze Position in Rigidbody, the sphere just went through the cube.
Here is the code I use to drag and drop (both objects have that script attached + rigidbody):
private bool dragging = false;
private float distance;
void OnMouseDown()
{
distance = Vector3.Distance(transform.position, Camera.main.transform.position);
dragging = true;
}
void OnMouseUp()
{
dragging = false;
}
void Update()
{
if (dragging)
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Vector3 rayPoint = ray.GetPoint(distance);
transform.position = rayPoint;
}
}