- Home /
Weird coordinates when cllicking on collider and convert to mesh point
Hi all
I'd like to find clicked point on my mesh.I wrote code:
void OnMouseUp()
{
RaycastHit hit;
if(Physics.Raycast(Input.mousePosition,transform.position,out hit))
{
infoText.text="hit on:"+hit.point.ToString();
}
}
well,it works but it gives me coords in world space,not mesh ones :/
I read docs,make research on this forum,ask few questions and find out that such shoud convert world's hitpoint coordinates into mesh local:
infoText.text="hit on:"+transform.InverseTransformPoint(hit.point).ToString();
But this gives me unacceptable results-if I move my mouse cursor about a centimeter in any direction I still get the same coordinates :/ Such issue does not appear if I check the point in world's space-change mouse position even a milimeter is noticeable and gives other point coords.
Who knows what can cause the issue?
Answer by MasterBLB · Jul 12, 2011 at 10:05 AM
I solved the issue on my own-there were too small scale set on FBX Importer.
Answer by Bunny83 · Jul 11, 2011 at 03:15 PM
I never had problems with that. Have you tried to log both the world and local coordinates? It's kinda inpossible that one is changing and the other stays. Btw: ToString is called implicitly when it's used like a string
infoText.text = "hit w:" + hit.point + " l:" + transform.InverseTransformPoint(hit.point);
And make sure you don't have "collapse" enabled at your console window.
I have no idea how it's possible,but I did as you suggested and logged both coordinates-as before,the world's ones changes and the mesh's not :/
And tell more please about that collapse option
The collapse button will hide similar messages. It happens regularly that people think their code is only executed once because there's only one line in the console. Sometimes it's useful when you still have a Debug.Log somewhere in Update().
I just realized that you use a GUIText or Text$$anonymous$$esh so the collapse button is irrelevant ;)
It sounds really strange, are you sure you use the right transform? You have to use the one of the $$anonymous$$esh. If you do a raycast from another object transform will be the transform of that other object and not the one of the $$anonymous$$esh.
Use
hit.transform.InverseTransformPoint(hit.point);
to be on the safe side.
I just saw that you use Physics.Raycast absolutely wrong ;). It works always in world space and the first argument have to be a position in 3d from where you want to start your raycast. The second argument is a direction, not a position.
In your case you would use Camera.main.ScreenPointToRay to get a Ray and use that one with Raycast.
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray,out hit))
{
[...]
The mouseposition is in screencoordinates and therefore only 2D ;)
I've created a test scene for your case and turned it into a web-build. The source package of the scene can be found at the bottom of the build.
Like i said i have no problems with the Raycasts. $$anonymous$$aybe you're a bit confused by the vector that is displayed when you use ToString() of a vector3. ToString will round x, y and z to 0.1 accuracy. $$anonymous$$y test scene displays the local and world coordinates two times. The first one is just Vector3.ToString and the second one is manually converted (component wise).
Since the sphere that comes with Unity has a radius of 0.5 all local coordinates will be between -0.5 and 0.5. $$anonymous$$eep in $$anonymous$$d that if you scaled the sphere by 100 so it has a radius of 50, the local coordinates are still only 0.5