Multiple gameobjects - one event

I want to drag-drop one object, but I have more such kind of objects and the behaviour happens for all, but I want to drag just the selected item. Some behaviours are working well, just for the selected object, but this one no.Please help me to improve this code.

void Start () {

	initialPosition = new Vector3 (transform.position.x,transform.position.y,transform.position.z);	
	
}
	void Update () {	
	

	
	if(Input.GetMouseButtonDown(0)){
		mouseDown = true;
	}
	
	if(Input.GetMouseButtonUp(0)){
		mouseDown = false;
		grabbed = false;
		
		
		this.transform.position = initialPosition;
	}
	
	if(mouseDown==true){
		point = Camera.main.ScreenToWorldPoint(Input.mousePosition);
		

		if(point.x >= (transform.position.x - renderer.bounds.size.x/2) && point.x <= (transform.position.x + renderer.bounds.size.x/2))
			if(point.y >= (transform.position.y - renderer.bounds.size.y/2) && point.y <= (transform.position.y + renderer.bounds.size.y/2))
				grabbed = true;
			
		if(grabbed==true){
			transform.position = new Vector3(point.x, point.y, transform.position.z);
			foodMovingPosition = transform.position;
			
			
		}
	}
		
}

You have no code there for selecting the object. Ironically, there is right now another question that is all wrong except selecting the object. Combine the two, and you'll both have success. http://answers.unity3d.com/questions/159503/drag-and-drop-ray-casting-conundrum.html

You can also use [OnMouseDown][1] (and Up) on non-mobile. Though I never have - those are less flexible than Physics.Raycast. [1]: http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.OnMouseDown.html

1 Answer

1

For your purpose, it’s better to use Raycast, since you must track the mouse pointer position. This script uses the variable pickObj to save the reference to the picked object. While nothing is picked, pickObj is null. When the left mouse button is pressed, it casts a ray from the mouse pointer and - if some object tagged “Pick” is hit - saves the object transform in pickObj (if the object is a rigidbody, its velocity is nulled to avoid weird movements when it’s released).

While the mouse button is pressed, pickObj is moved to the mouse pointer position. Since the screen is 2D, the script uses the distance from the camera (ray origin) to the object to define a 3D point with GetPoint(distance). When the mouse button is released, pickObj is set to null to signal nothing is picked.

private var pickObj: Transform = null;
private var hit: RaycastHit;
private var dist: float;

function Update(){
	if (Input.GetMouseButton(0)){
		var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
		if (!pickObj){ 
			if (Physics.Raycast(ray, hit) && hit.transform.tag == "Pick"){ 
				if (hit.rigidbody) hit.rigidbody.velocity = Vector3.zero;
				pickObj = hit.transform; 
				dist = Vector3.Distance(pickObj.position, Camera.main.transform.position);
			}
		}
		else {
			newPos = ray.GetPoint(dist); 
			pickObj.position = newPos;
		}	
	}
	else { 
		pickObj = null;
	}
}

I would normally argue that the Raycast should only be done upon GetMouseBittonDown, since otherwise it is click-then-drag-onto-target-then-drag, which is an unusual mechanic, but for small and/or jittering targets it might actually be an advantage. +1

In my first version I used GetMouseButtonDown/Up and a flag to start and end the dragging, but after some time I noticed that the whole thing was doing the same job GetMouseButton could do alone.

@Alexis100, you can use Lerp to make the object follow the mouse pointer in a nicer fashion - just change the line where pickObj.position is set: <pre> pickObj.position = Vector3.Lerp(pickObj.position, newPos, Time.deltaTime * speed); </pre> You must declare speed at the beginning of the script - something like var speed: float = 5.0; PS: Brasileiro também?

I realize this is a response a year and a half later, but I'll take a chance. I implemented your code, however have been struggling to get the Z axis to interact. I mapped it to move at a reduced rate of the y coordinate, but it's very jittery. Is there an alternate solution that you would propose?

I didn't understand your problem: this script works fine in 3D, since it doesn't discriminate axes - are you trying to use this script in a 2D game?