- Home /
How to get the center of a collider face and the halfExtents to the corners of that face?
So my original plan was to get this this info with a racasthit, look for the center of the collided line (or rectangle if it´s 3d) and then just compare how close it was to the edges also getting the halfExtents. But there raycasthit doesn´t have any properties to get the center or edges, and am not even sure if that´s possible.
So the green outline would be the collider and the red dots the positions I want to get. As you can see, they aren´t the edges of the collided rectangle/triangle. They are the corners of each straight line composed by the collider. Is there a way of doing this?
This is to check if the player is grounded. I was originally doing a checkBox, but is not accurate enough to check for very inclined plane collisions. (In fact, the y scale needs to become very big if it´s very close to 90 degrees). I could increase the x scale, but then the object would realize late that is not on a platform once it reaches the end. Please don´t suggest anything related to physics system: am not using it. (And I tried OnCollisionExit and it still gave undesired effects). Besides, this solution of getting the corners of a straight line also solves other issues, like if the player is on the slope, but the checkBox isn´t (if it´s too inclined and the collision point is on the arm, for example).
If there´s no way of getting the collider vertex, the mesh vertex still works.
Is there scope to reduce the resolution so that it is one face? ins$$anonymous$$d of 9 (for just the top edge)? i.e. two triangles. It would probably be a good optimisation as having that many polys to define a straight line (what is essentially a quad) may be a bit unnecessary.
It would also make this and other problems trivial.
That was just an example. I know that in this specific case is a bit unnecessary to have that many polys, but in other cases where the geometry might be complex for whatever reason you can´t do that. And still, how to get the distance between those vertices? The collider apparently doesn´t have that info and if they aren´t the max distance you can´t get them checking the scale.
And a straight line isn´t necessarily composed by a single edge (see the lowest line). To make that kind of polygon, you need to break it down to a bunch of quads and triangles (am not entirely sure if that image is completely accurate, but it proves my point).
I thought to maybe decompose that polygon into separate pieces and then parenting them, but I don´t think that colliders would match it perfectly and I would end up spending more resources than necessary. In the previous case I would normally use a mesh collider; but if it´s separated I would need more of them, since box colliders don´t work with slopes (and I think that having several mesh colliders imitating the same figure than a single mesh collider is worst for performance, since you are increasing the amount of vertices of an already expensive collider, but am not entirely sure of this).