Strange problem with Drag Script

I have a script that allows me to drag objects, the problem is that as I move the selected object up/down it seems to go further into/out of the screen.

private var currTarget: Transform;
private var dragging: boolean;
private var clickOffset: Vector3;


function Update () {
	var objPlane: Plane;
	var hitDist: float;
	var hit: RaycastHit;


			if (Input.GetMouseButton(0))  {
		ray = Camera.main.ScreenPointToRay(Input.mousePosition);

		if (dragging &currTarget) {	
			objPlane = new Plane(-Camera.main.transform.forward, currTarget.position);
			objPlane.Raycast(ray, hitDist);
			currTarget.position = ray.GetPoint(hitDist) + clickOffset;
		} else if (Physics.Raycast(ray, hit)  Input.GetMouseButtonDown(0)) {
			dragging = true;
			currTarget = hit.transform;
			
			objPlane = new Plane(Camera.main.transform.up, currTarget.position);						objPlane.Raycast(ray, hitDist);
			clickOffset = currTarget.position - ray.GetPoint(hitDist);
		
	} else {
		dragging = false;
	}
	}
}

Any ideas on why this could be ?

I’ve tried playing around with objPlane = new Plane(-Camera.main.transform.forward, currTarget.position); trying right and up and negitive right and up but I haven’t had much success.

It seems to be ok when I go left and right, its only when I move up/down that seems to cause the problem?

I think the problem has something to do with the object selected sometimes moving in the z-axis as in trying to move towards the camera rather than being confined to just the x and y-axis, is there a way to fix this?

Sorry to be a pain but I’ve not had much luck with this.

Any thoughts on what could be done?

You need to use the camera’s transform.forward vector as the plane’s normal rather than transform.up.

Hi andeeee,

I tried changing my code to this like you suggested:

private var currTarget: Transform; 
private var dragging: boolean; 
private var clickOffset: Vector3; 


function Update () { 
   var objPlane: Plane; 
   var hitDist: float; 
   var hit: RaycastHit; 


         if (Input.GetMouseButton(0))  { 
      ray = Camera.main.ScreenPointToRay(Input.mousePosition); 

      if (dragging &currTarget) {    
         objPlane = new Plane(-Camera.main.transform.forward, currTarget.position); 
         objPlane.Raycast(ray, hitDist); 
         currTarget.position = ray.GetPoint(hitDist) + clickOffset; 
      } else if (Physics.Raycast(ray, hit)  Input.GetMouseButtonDown(0)) { 
         dragging = true; 
         currTarget = hit.transform; 
          
         objPlane = new Plane(-Camera.main.transform.forward, currTarget.position);                  objPlane.Raycast(ray, hitDist); 
         clickOffset = currTarget.position - ray.GetPoint(hitDist); 
       
   } else { 
      dragging = false; 
   } 
   } 
   
   if (dragging == false)
    {
     	 // Move the object 
    	var translation = Time.deltaTime * 5;
    	transform.Translate (translation, 0, 0);   
    }
}

Although it works the problem is if I click somewhere a cube will snap to that position for some reason?