How can I get mouse cursor position in 3D space ? And how can I get mouse cursor land position ?
You can use Camera.ScreenPointToRay() to get a ray going from a point on the screen (use Input.mousePosition as the point), then cast that ray using the physics system Physics.Raycast(). You should be able to get all the data you need with those three functions, and theres a good example in the Input.mousePosition link there.
Thank’s
PS.
If it not a problem can you give me some code example for this.
Have a look at this thread for further explanation and a code sample.
i had this problem before, and i read the thread that andeeee gave you
i came up with this code:
function GetClickedPoint() : Vector3 {
var ray : Ray = Camera.main.ScreenPointToRay(new Vector2(Input.mousePosition.x,Input.mousePosition.y));
var hit : RaycastHit;
if(Physics.Raycast(ray, hit))
return(hit.point);
}
now if u want to figure out the coordinates of the point at which the user clicked, just call the function…
one thing you should keep in mind though: this code will work if the user clicks on an object in the 3D space, it wouldn’t work if the mouse was not pointing over nothing when it was clicked
hope this helps
So if I understand well this line of code detected which object on the scene are in itersection with mouse cursor :
if (Physics.Raycast(ray, out hit)) {
if (hit.transform == target1) {
Debug.Log("Hit target 1");
} else if (hit.transform == target2) {
Debug.Log("Hit target 2");
}
} else {
Debug.Log("Hit nothing");
}
Where or better question how I pass values to this variables :
public Transform target1, target2;
so I can compare transform values .
And another question is :
Where I put this script , in Camera object or somewhere else ?
Thank’s for help
These are public variables, so they will be visible in the inspector. You can just drag the target objects onto the variables. Note that this is just an example of what you can do - any technique for comparing the objects is fine.
You can put this kind of code anywhere. Often with code like this, it is useful to create an empty “supervisor” object in the scene and attach a script to that. This object won’t display or move but the script will still operate.
So if I have for example mesh created in blender and that mesh are constructed with several objects , for example a robot where arm, leg , head are objects but in the scene there are only as one mesh. How can I detect which object in the mesh are collide with some other object in other mesh. Better is there any function for object enumeration in mesh.
PS.
Is there some example of creating and using decals ?
You can iterate over the child objects of a parent using the parent’s transform. There is an example of this at the top of the Transform class doc page.
This subject has certainly been discussed on the forum before. Have a search, and start a new thread under Unity Support if you don’t find an answer.
Thanks a lot