- Home /
Question by
noblerare · Sep 23, 2016 at 06:02 PM ·
dragdraggingdrag objectsdrag-rigidbody
Mouse drag GameObject flies away
Hi, I have a few spheres on a canvas that I want to drag around with the mouse. They drag fine. Even when I collide two spheres together. The problem comes when I have two spheres close to each and take a third sphere and collide them. Then they all start moving on their own and even sprouting wings and flying out of the area. How do I prevent this? I just want the spheres to stay static and move out of the way when another sphere hits them.
Here is my Drag script that is attached to every sphere.
void OnMouseDown()
{
screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
}
void OnMouseDrag()
{
Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
transform.position = curPosition;
}
Comment