Drag GameObject by using mouse

Hello everybody;

i want to an object drag but the object does not detect collision or collider in 3D space.

i have a code for dragging on the gameObjects;

// variable definitions

var planeNormal : Vector3 = Vector3( 0, 1, 0 );
var currentProjectedOnPlaneNormal : float;
var cameraProjectedOnPlaneNormal : float;
var wantedDistanceFromCamera : float;
var pOnPlaneNormal : float;
private var rememberedYPos: float;

var controlDrag:boolean;

/* built-in functions */
// Use this for initialization
function Start()
{   

controlDrag=true;
}

function Update () {
}

function OnMouseDown ()
{

controlDrag=true;
// An extra hack: If Up is (0,1,0), then we remember the y-coordinate, so we don't change it
rememberedYPos = transform.position.y;
}

function OnCollisionEnter(collision:Collision){

	
	if(collision.gameObject.tag=="Duvar"){
		
		controlDrag=false;
		
	

	
	}
	
	
}

function OnCollisionExit(collision:Collision){

	if(collision.gameObject.tag=="Floor"){
	
	
	}


}

function OnCollisionStay(collision:Collision){

	if(collision.gameObject.tag=="Wall"){
		
		controlDrag=false;
		
	

	
	}


}

function OnMouseDrag () 
{
	
		if(controlDrag){
		
		Drag();
		}
	
	
}




/* user defined functions */

function FindCamera ()
{
  if (camera)
  return camera;
  else
  return Camera.main;
}

function Drag()
{
var p: Vector3;


var mainCamera = FindCamera(); 



// Position on the near clipping plane of the camera in world space
p = mainCamera.ScreenToWorldPoint(Vector3(Input.mousePosition.x,Input.mousePosition.y, mainCamera.nearClipPlane));
// Position relative to the eye-point of the camera
p -= mainCamera.transform.position;

currentProjectedOnPlaneNormal = Vector3.Dot( planeNormal, transform.position );
cameraProjectedOnPlaneNormal = Vector3.Dot( planeNormal, mainCamera.transform.position );
wantedDistanceFromCamera = cameraProjectedOnPlaneNormal - currentProjectedOnPlaneNormal;        
pOnPlaneNormal = Vector3.Dot( planeNormal, p );

p *= wantedDistanceFromCamera / -pOnPlaneNormal;
p += mainCamera.transform.position;
transform.position = p;

// If we know that the object should only move in its x,z coordinates, we can
// make sure we don't drift slowly off the plane by doing this:
transform.position.y = rememberedYPos; 
}

i want to move game object with the same time mouseCursor. And also the gameObjects should detect the colliders and they do not go out the Walls.

In shortly, Not only the gameobject move the floor like "Transform movement " but also detect any physics around its(Wall or another game object).:face_with_spiral_eyes:

Any help i will be very appriciate…

i want to add some information about dragging. The rigidbody should detect the collisions and also it does not pass to outside of the Room (Wall). Also The rigidbody should be limited its velocity. But how can it possible ? How am i put it this code…?

Don’t know if you’ve seen it already but there is a script that comes with the standard package for dragging objects with a Rigidbody attached.

You are moving the object via transform.position. This is equivalent to teleporting as far as PhysX is concerned. It therefore knows nothing about the motion of the object and can therefore not do any collision checks.

To include PhysX in the game again, in stead move the object via the rigidbody - using Rigidbody.MovePosition from FixedUpdate. Alternatively you could use forces.

1 Like

Thanks for answers.

I had a similar issue. After tinkering unifycommunity.com - unifycommunity Resources and Information., I got the results I needed. Also, in the standard assets there is a script called DragRigidbody.

I cannot understand you. Could you please rephrase ?

I found this page usefull : Unity - Manual: Box collider component reference, especially Collision action matrix at the bottom of the page. Perhaps a Kinematic Rigidbody Trigger Collider is what you need.

I believe it is covered by the two scripts I mentioned.

1 Like