- Home /
Push object away from edge/corner
I'm trying to place portals to the mouse click positions. How do I make sure that portal completely overlaps underlying plane when user clicks too close to the edge or corner? Here is an example.
The only solution I can think of is create another collider mesh (similar to nav mesh) where each wall or floor is shrunk (colored with blue on picture). And when user clicks on real wall, find nearest point on shrunk mesh and place portal there. But then I have to create manually "nav mash". Is there easier way to place portal correctly?
Find nearest point from MeshCollider work only with convex. But in my case its just multiple planes separated from each other. So I'll have to manually iterate through triangles to find nearest point.
Any other suggestions would be greatly appreciated. :)
Answer by Xeon-13 · Mar 20, 2018 at 07:16 AM
I think I found a solution using RayCast + SphereCast.
RayCast mouse click to find hit with wall
SphereCast from above that hit point to find hit with "nav mesh"
Use point from 2nd hit and normal from 1st hit to place a portal
Ray ray = Camera.main.ScreenPointToRay(click); RaycastHit rayHit; if (Physics.Raycast(ray, out rayHit, Mathf.Infinity, NOT_NAV_MESH_MASK)) { RaycastHit sphereHit; if (Physics.SphereCast(rayHit.point + 1.001f * rayHit.normal, 1f, -rayHit.normal, out sphereHit, Mathf.Infinity, NAV_MESH_MASK)) { OpenPortal(sphereHit.point, rayHit.normal); } }
Here is port mesh drawn with green lines.
Answer by DarkToadster · Mar 17, 2018 at 06:33 AM
Hey there ! I've made a small script using Bounds (Link to Docs).
You can download my scripts as an .unitypackage including an example !
Let me know if you need any help :)
Your answer
Follow this Question
Related Questions
Is there any way to find connected vertices in a mesh which was imported from Blender? 1 Answer
Starter Asset Capsule gets stuck on corners 0 Answers
Is it possible to translate a NavMesh at runtime? 2 Answers
GameObjects on terrain change position at each execution 0 Answers
I need help about raycast and edges 2 Answers