Moving GameObjects with Mouse (457381)

Hallo!

How can I move an object (a cube for example) to a certain target point by clicking on the object and holding down the left mouse button?

Main idea is to move certain objects to their correct places.

Thanks in advance

Maria

Hi,
You can use this on the Objects.

function OnMouseOver ()
{
if(Input.GetMouseButton(0))
transform.position = Input.mousePosition;

}

I can’t try to run it now, sorry i don’t know if it works

:-((( The moment I click on the object , it disapears out of terrain

Hmmm… His post seems to dump the object at the exact point where the mouse is on the screen, but in 3d space… So if the mouse is in the upper right corner, it is at 0,0,0… meaning your object is at 0,0,0… this does not take into account where the camera is in space nor anything like that.

So, lets first clarify exactly what you are doing. You want to click and drag an object and drag it…

Lets improve upon the previous post.

First, we need to know when the mouse is down on something. In the previous post, it only moves when the mouse is over it, and pressed. His immediately moved it away from the camera, so it then stopped moving it.

To do this we will use a variable that holds the object we clicked on. We will also make sure that this object is tagged correctly. So if we tag it as movable, then it is movable.

private var pickedObject : Transform;

function Update(){
	var ray : Ray;
	var hit : RaycastHit;
	var hits : RaycastHit[];
	
	if(Input.GetMouseButtonUp(0))
		pickedObject = null;
	
	// if the mouse button is down (one time event)
	if(Input.GetMouseButtonDown(0)){
		pickedObject = null;
		
		// get a ray from where the mouse is outwards from the camera
		ray = Camera.main.ScreenPointToRay(Input.mousePosition);
		
		// do a physics check to see if we hit an object that is tagged correctly
		if(Physics.Raycast(ray, hit)){
			if(hit.transform.root.gameObject.tag == "Movable")
				pickedObject = hit.transform.root;
		}
	}
}

OK, now we can go about the task of moving it… how, is now dependent on what you are doing. We can either use a Plane or RaycastAll.

A Plane will give us a flat surface to move things on. RaycastAll will give us all the hits possible. All we would have to do is find the first one that is not the object we are looking for. I will give you the code off for a RaycastAll.

private var pickedObject : Transform;

function Update(){
	var ray : Ray;
	var hit : RaycastHit;
	var hits : RaycastHit[];
	
	if(Input.GetMouseButtonUp(0))
		pickedObject = null;
	
	// if the mouse button is down (one time event)
	if(Input.GetMouseButtonDown(0)){
		pickedObject = null;
		
		// get a ray from where the mouse is outwards from the camera
		ray = Camera.main.ScreenPointToRay(Input.mousePosition);
		
		// do a physics check to see if we hit an object that is tagged correctly
		if(Physics.Raycast(ray, hit)){
			if(hit.transform.root.gameObject.tag == "Movable")
				pickedObject = hit.transform.root;
		}
	}

[COLOR="red"]	// check to see if the button is down (every frame event)
	if(Input.GetMouseButton(0)  pickedObject){
		ray = Camera.main.ScreenPointToRay(Input.mousePosition);
		hits = Physics.RaycastAll(ray);
		for(var i=0; i<hits.Length; i++){
			if(hits[i].transform.root != pickedObject){
				pickedObject.position = hits[i].point + hits[i].normal;
				pickedObject.up = hits[i].normal;
				i = hits.Length;
			}
		}
	}[/COLOR]
}

OK, so the concept is, you put your mouse over something and it grabs it and moves it over a terrain, or whatever. Now this code can only be in the script ONCE… so put it on a controller object, not every object you want to drag.

This code has been tested and works fine.

OUAAAOU!!! It is indeed working. How can I thank you enough! But I should be more specific as to what I am supposed to do. I have the letters of a word (written on wooden crates) scattered in random places. The player has to pick each letter/crate and drag it to the correct place so that he completes the word. An icon of the word is rotating above the target place for the letters/crates, letting the players understand which word it is and therefore which letters they should look for! If a letter is delivered to the correct place then the sound of correct is being heard, otherwise the sound of wrong! It is supposed to be for children! I am a teacher, so… ! I do not want to be a burdain on you! I imagine you have a million things to do but this is really important for me so I would like to pay you. Please let me know how much it would cost! Thank you so much

Maria