I´ve tried for some hours to create a script that simply translates the clicked object with the mouse button along the X and Y axis. After several searches a tries, I just can´t understand how to do it.
The best I can do was follow this code:
public Vector3 point;
void OnMouseDrag() {
point = Camera.main.ScreenToWorldPoint(Input.mousePosition);
transform.position = point;
}
Also I tried to use raycast, but can´t realize how to return the hit object. The real problem is that I can´t make the object follow the mouse. Sometimes It just “jumps” for other location.
Well, the basic idea for a drag and drop script would be as follows:
Check to see if the mouse is over the draggable object by using a Raycast.
If the mouse is over the object, check to see if the mouse is clicked
Then as long as the mouse is clicked, move the object to the mouse position. Make sure that for this part that the mouse does not need to be over the object. The mouse needs to be over the object when the mouse is first clicked, but for as long as the mouse is held it does not need to be over the object. To move the object on a plane just change the x and y coordinates of the object’s position, rather than all three coordinates.
void OnMouseDown()
{
distance = Vector3.Distance(transform.position, Camera.main.transfrom.position);
dragging = true;
}
it says: error CS1061: Type UnityEngine.Camera' does not contain a definition for transform’ and no extension
method transform' of type UnityEngine.Camera’ could be found (are you missing a using directive or an assembly reference?)