Question

I'm trying to get the exact location of click in Unity using C#, but I have no idea how to determine it. I tried using raycast and I get a quite accurate location, but it's just rounded to one decimal. It is a little bit too big of a mistake for me. So how can I get a totally accurate location?

My code is as follows:

RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(holdPosition);
if (Physics.Raycast (ray, out hit, Mathf.Infinity, 1<<15)) {
    Debug.Log(hit.point);
}

And I get a result like (0.5, 1.3, -5.0), but I want a precision of up to 4 or 5 decimals.

Était-ce utile?

La solution

You actually do get more precise results than that. Values in the console are rounded. The actual floating point values do have a higher precision. If you want to see what that is, you could use a format specifier like:

Debug.Log(hit.point.ToString("F4"));

Where "F4" is a standard numeric format string. That should show you more precise values.

Autres conseils

Debug.Log(hit.point.x);
Debug.Log(hit.point.y);
Debug.Log(hit.point.z);

Will give you more precise values.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top