# Objects do not stop when colliding each other while using mouse drag

I work on a 2D game and I have multiple objects that the player can control.

The objects each have a RigidBody2D and a Collider, and a method OnMouseDrag() with which I can drag the objects.

I want the dragging to stop when the objects hit each other.

I tried by using this:

rigidbody2d.velocity = Vector3.zero;
rigidbody.isKinematic = true;


But the objects do not stop.

I want exactly like this video. I tried this but it isn't work too.

using UnityEngine;
using System.Collections;
public class NewBehaviourScript1 : MonoBehaviour
{

bool allowDrag = true;
void OnCollisionEnter(Collision collision)
{
allowDrag = false;
}

void OnMouseDrag()
{
if(allowDrag)
{
float distance = 7;
Vector3 mousePosition = new Vector3 (Input.mousePosition.x, Input.mousePosition.y , distance);
Vector3 objPosition = Camera.main.ScreenToWorldPoint (mousePosition);
transform.position = objPosition;
if ((transform.position.x <0))
{
Vector3 _tmp = transform.position;
_tmp.x = 0;
transform.position = _tmp;
}
if ((transform.position.x > 4.5f))
{
Vector3 _tp = transform.position;
_tp.x = 4.5f;
transform.position = _tp;
}

}
}
void OnMouseUp()
{
if ((transform.position.x > 0) && (transform.position.x < 0.375f))
{
Vector3 _tr = transform.position;
_tr.x = 0;
transform.position = _tr;
}
if ((transform.position.x > 0.375f) && (transform.position.x < 1.25f))
{
Vector3 _fs = transform.position;
_fs.x = 0.75f;
transform.position = _fs;
}
if ((transform.position.x > 1.125f) && (transform.position.x < 1.875f))
{
Vector3 _tr2 = transform.position;
_tr2.x = 1.5f;
transform.position = _tr2;
}
if ((transform.position.x > 1.875f) && (transform.position.x < 2.625f))
{
Vector3 _tr4 = transform.position;
_tr4.x = 2.25f;
transform.position = _tr4;
}
if ((transform.position.x > 2.625f) && (transform.position.x < 3.375f))
{
Vector3 _tr6 = transform.position;
_tr6.x = 3;
transform.position = _tr6;
}
if ((transform.position.x > 3.375f) && (transform.position.x < 4.125f))
{
Vector3 _tr7 = transform.position;
_tr7.x = 3.75f;
transform.position = _tr7;
}
if ((transform.position.x > 4.125f) && (transform.position.x < 4.875f))
{
Vector3 _tr8 = transform.position;
_tr8.x = 4.5f;
transform.position = _tr8;
}
}
void Start ()
{
}
void Update()
{
}


}

The drag movement is not being applied through a force on the rigidBody but directly on the object's transform position, so what you are trying to do wont work. Instead you need to detect the collision and then disable OnMouseDrag with a boolean value sort of like this:

bool allowDrag = true;

void OnMouseDrag() {
if(allowDrag)
{
//... do something
}
}

• many thanks for help but it not work because drag script common component the all object. – cagriarpacik Jan 17 '17 at 16:23
• what do you mean? Be more specific on what you mean by "all object" and I can probably help. – Tartle Wizard Jan 17 '17 at 17:46
• youtube.com/watch?v=ZfjVR-0ZFHU&t=270s like this video. But the player control spehere and block individualy. I wrote OnCollision changing the allowDrag = false; But it not work. I m pretty new on Unity Sorry. – cagriarpacik Jan 17 '17 at 19:02
• His second version of the Update is moving the object with velocity. I don't see him using OnMouseDrag. You may need to post your code. – Tartle Wizard Jan 17 '17 at 19:19
• I wrote the code upthere. And multiple object have the script. Thanks for help. – cagriarpacik Jan 17 '17 at 19:26