Help With Drag and Drop

Hi guys i need help with a drag and drop script.
I found this script online - credits to whoever made it.

// -------------------------------------------------------------------------------------------------
// DRAG AND DROP 3D OBJECTS IN AN ORTHOGRAPHIC VIEW
// X AND Z
// SNAP OBJECT TO LOCATION in X  Z
//
// SNAP TO LOCATION VIA OCCUPIED LOGIC
// SNAP TO LOCATION VIA ARRAY SHUFFLE
// SNAP TO TWO DISTINCT LOCATIONS
// GAME BOARD LOCATIONS

// HOUSE LOCATIONS

// EACH LETTER HAS A HOUSE, GAMEBOARD AND A CARD REPRESENTING THAT LETTER

// HOUSE OF A
// CARD OF A
// GAMEBOARD OF A



// --------------------------------------------------------------------------------------------------




// CHICKEN DRAG OBJECTS SCRIPT FROM UNITY3D FORUMS

#pragma strict 

// Attach this script to an orthographic camera. 

private var chicken : Transform;      // The chicken we will move. 

private var offSet : Vector3;      // The chicken's position relative to the mouse position. 

function Update () { 

   var ray = camera.ScreenPointToRay(Input.mousePosition);
   // Gets the mouse position in the form of a ray. 
   
   
   
   
   if (Input.GetMouseButtonDown(0)) {      // If we click the mouse... 
   
      var hit : RaycastHit; 
   
      if (Physics.Raycast(ray, hit, Mathf.Infinity)) {      // Then see if an chicken is beneath us using raycasting. 
   
         chicken = hit.transform;      // If we hit an chicken then hold on to the chicken. 
   
         offSet = chicken.position-ray.origin;      // This is so when you click on an chicken its center does not align with mouse position. 
   
         if (chicken.rigidbody) { 
   
            chicken.rigidbody.isKinematic = true; 
   
         } 
   
      } 
   
   } 
   
   
   
   else if (Input.GetMouseButtonUp(0)) { 
   
      if (chicken.rigidbody) { 
   
         chicken.rigidbody.isKinematic = false; 
   
      } 
   
      chicken = null;      // Let go of the chicken. 
   } 
   
   
   if (chicken) { 
   
      chicken.position = Vector3(ray.origin.x+offSet.x, chicken.position.y, ray.origin.z+offSet.z);
      // Only move the chicken on a 2D plane. 
   } 
}

This is the original.

My problem with it is that it moves any object that i click on i have tried a few thngs and none of them work. all i want is it to be able to move specific objects and not others. i hope you understand.

Also this is for a 2d game, and i only want rigidbodys effected.

ok i fixed my problem with the whole what objects to move and what not to move. but i still need a collision part so when i move the objects they don’t go through another object. ill try and figure that out also. but any help would be appreciated
:smile:

so basically i still need help with collision. please help.

are you using coliders on your rigidbodies?

did you solve the pickup problem with layers or in another way that might (or not) interfere with the rest?

I might be mistaken now, but I notice that the script you use for drag and drop translates the object by setting the position each frame if “chicken” is not null. You see, when “chicken” this script will set the position of this object to the value every frame thus ignoring any force set to the rigidbody from collisions. If you want this to collide and receive and add force to other rigidbodies I reckon you can do it at least in two ways. The first is to not set the position of “chicken”, but adding force to it to get it where you want. This method is probably not what you want because it might be tricky to make it look good and feel natural(possible though) and the second I can think of is to Physics.OverlapSphere to check for other colliders and then adding ExplosionForce or something on the other objects.

It’s late and I might’ve rambled without answering your question, but I think this is you problem.