- Home /
Raycast stops on a mesh collider on empty parts of the collider.
I found a problem in my game where the raycasting doesn't seem to work with the mesh colliders very well, it seems to collide with them not only on their surface, happens in more cases then the examples i show here and happens constantly in these places so it is not a passing behaviour.
the code:
if (Input.GetKeyDown(KeyCode.Mouse0))
{
RaycastHit Hit;
Ray R = new Ray(gameObject.transform.position, transform.forward);
Physics.Raycast(R, out Hit, Max);
if (Hit.collider != null && Hit.collider.tag != "Object" && Hit.collider.tag != "Slide" && Hit.collider.tag != "Floor" && Hit.collider.tag != "NoSoundFloor" && Cursor.lockState.Equals(CursorLockMode.Locked))
{
LR.enabled = true;
LR.SetPosition(1, Hit.point);
// trKek.position = Hit.point;
temp.transform.position = Hit.point;
confirm = true;
gp = true;
tr.GetComponent<Rigidbody>().freezeRotation = false;
h.xMotion = ConfigurableJointMotion.Locked;
h.yMotion = ConfigurableJointMotion.Locked;
h.zMotion = ConfigurableJointMotion.Locked;
h.anchor = temp.transform.localPosition;
// lastTime = Time.time;
}
Debug.LogError(Hit.collider.name + "; " +Hit.distance + "; " + Hit.point);
}
and here is one instance of it happening:
Last thing i must note is that the raycast IS hitting the mesh collider for sure as in the Debug it shows the mesh collider's name under Hit.collider.name I would be very happy to get any help on the matter.
Answer by rnhfjkui · May 22, 2016 at 01:39 AM
FIXED IT if anyone is interested the imported mesh collider MUST be collada type because FBX was making the problem. i don't know why tho it is kind of weird but everything is fixed.
You, my friend, have saved me more than you can imagine. Thank you, this fixed it for me too.
Omg!! You life saver. I nearly gave up a whole dream until I saw this solution. Thank you so much. I will mention you in the thank you credits (if I ever get there) lol.
Answer by S4UC1SS0N · Aug 23, 2016 at 05:53 PM
Thank you so much for this ! It was making me crazy for a week ! You rock.
Your answer
Follow this Question
Related Questions
Ray cast not hitting the backside of mesh collider attached to a spherical mesh object 0 Answers
Find closest point on all objects in a layermask 1 Answer
My characters in Unity float slightly above my ground I imported and made in Blender 0 Answers
Physics.Boxcast not working with mesh collider 1 Answer
Raycasting not working on a procedural mesh with a mesh collider. 1 Answer