Hi everyone, im trying to make a script that when i click on a mouse with the mouse, the user will be able do drag it around while he holds the button. Here is my piece of code: Code (csharp): function Update () { if (Input.GetMouseButton(0)) { var hit : RaycastHit; var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, hit)) { mousePos = Vector3(Input.mousePosition.x, Input.mousePosition.y, 10); hit.transform.position = Camera.main.ScreenToWorldPoint(mousePos); } } } I'm having some problems, fist problem: I'm setting a fixed Z to the mouse position and i want the object to stands in its original z axis. Any insights about that? I've tried some other things but it didnt work properly. Second problem: I want the first click to doesnt move the object, the first just to select and after the click you move the mouse and then the object comes along. This way, any click the objects moves a little. Thank you,
Well for problem 1, why not just use the Z position of the object when assigning mousePos? mousePos = Vector3(Input.mousePosition.x, Input.mousePosition.y, hit.transform.position.z); As for problem 2, you should have a Transform variable for the object selected, let's call it m_SelectedObject, on raycast hit, the hit.transform should be assigned to m_SelectedObject. If the raycast does not return a hit, assign m_SelectedObject to null. Then in the update function outside of the raycast code, have this: Code (csharp): if(m_SelectedObject != null) { mousePos = Vector3(Input.mousePosition.x, Input.mousePosition.y, m_SelectedObject.transform.position.z); m_SelectedObject.transform.position = Camera.main.ScreenToWorldPoint(mousePos); } So basically you check whether you have anything selected, if you do move it to the position of the mouse (while maintaining its original Z position)
I cant use like this: Code (csharp): mousePos = Vector3(Input.mousePosition.x, Input.mousePosition.y, hit.transform.position.z); Because afterwards i do this: Code (csharp): hit.transform.position = Camera.main.ScreenToWorldPoint(mousePos); And it screw things up bringing the object to the camera Z position. Any ideias?
I've noticed that on the mousePos.z i have tu put the difference between my camera.z and my object.z and it works Like this: Code (csharp): var desiredZ: float = Mathf.Abs(Utils.Round((Camera.main.transform.position.z - hit.transform.position.z),2)); mousePos = Vector3(Input.mousePosition.x, Input.mousePosition.y, desiredZ); hit.transform.position = Camera.main.ScreenToWorldPoint(mousePos); It works, but does anyone have a more ellegant solution?
Camera.main.ScreenToWorldPoint() needs a camera relative z position does it not? If you just give it the Z position 0, then it will return the position fo the camera. The Z position you want to use is the distance between m_SelectedObject and the camera. Assuming the camera is looking along the z axis. Code (csharp): m_SelectedObject.transform.position = Camera.main.ScreenToWorldPoint(mousePos.x, mousePos.y, m_SelectedObject.transform.position.z - Camera.main.transform.position.z); edit: Beat me to it Gilson!
This thread pertains to my interests. On the note of objects being selected, just add a selectedObject variable, and if(selectedObject != hit.raycast.IforgotwhateverXD) {selectedObject = hit.raycast.bluh; /*test for mouse release before continuing*/} else {/*your already written logic here*/} Sorry if this doesn't help, but in any case I'm trying to do something similar, or rather I will be in the near future.