Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ObjectRaycast distance algorithm does not scale back #15

Open
TheMax2000 opened this issue Nov 25, 2016 · 0 comments
Open

ObjectRaycast distance algorithm does not scale back #15

TheMax2000 opened this issue Nov 25, 2016 · 0 comments

Comments

@TheMax2000
Copy link

pb_HandleUtility.ObjectRaycast returns normally the closest object hit by the ray. However, when using MeshRaycast, it transfers the ray to local coordinates, and then return the "local computed distance". When using scaling, this can end up to wrong object selection. The distance must be scaled back to a common base (global coordinates, or percentage) before being compared to other distance.

Here the code to scale back to global

public static bool MeshRaycast(Mesh mesh, Ray ray, out pb_RaycastHit hit, Transform transform)
{
...
if(pb_Geometry.RayIntersectsTriangle(ray, a, b, c, Culling.Front, out dist, out point))
				{
					hit = new pb_RaycastHit();
					hit.point = point;
					hit.distance = Vector3.Distance(transform.TransformPoint(hit.point), transform.TransformPoint(ray.origin));
					hit.normal = Vector3.Cross(b-a, c-a);
					hit.triangle = new int[] { triangles[i], triangles[i+1], triangles[i+2] };
					return true;
...
}

I think you could return distance as a percentage too, so you wouldn't need to pass the transform to the function. However it means that your ray have a certain defined length, which is usually not the case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant