Make an object collide with certain other objects when moved to RaycastHit point?

I’m making a simple building system using Raycasting from the mouse pointer. The object that is to be placed is moved (and cloned on click) to the RaycastHit.point of the ray, but I want it to fully collide with objects of its own type, shifting its position relative to he hit point. The object can intersect the terrain as shown in the gif, but should not be inside of the already placed wall blocks. I tried using hit.distance but couldn’t figure the detection out, since the object is being positioned at the collision point already. Should I try and detect how should the position should shift relative to the hit point or should I somehow make the MeshCollider work while it’s being constantly moved to the RaycastHit.point?
alt text


If I understood you correctly then I would prefer shifting position solution. As I can see from the gif your pivot point GameObject is somewhat y = 0 and x = 0.5 related to the mesh size. To make sure you are not placing this object inside other already placed GameObject you will need offset it’s position. To make it’s only for the same type of object (so you can intersect terrain, but not already placed wall blocks) you’ll need to detect them. Therefore I would suggest you to try and do this:

  • When receive RaycastHit info check for CompareTag to make sure it’s wall block;
  • Calculate the normal to understand at what side you are pointing (this is important, becase, if you are placing on top of object and because your y pivot of GameObject is 0 then you don’t need to shift it, right? But along horizontal you will need to always shift to 0.5 of mesh size position, however if you are pointing at down side of GameObject and since your y pivot is 0 then you should shift 1 of mesh size. Keep in mind that it will depend on pivot and currently I am talking about your gif). If you are not familiar with normals then here is good explanation of what normal is: What is a RaycastHit normal? - Questions & Answers - Unity Discussions
  • Based on the normal direction, multiply your calculated normal to appropriate size of mesh, for example based on gif if it’s horizontal it would be 0.5 of mesh size, if vertical UP direction then no shift need and if vertical DOWN then 1 of mesh size;
  • Add calculated shift to hit.point and place give that info to transform position.

What I would like to add - look at the image158779-ex.png
There is situation where you are placing your wall and shifting properly, but since there is already placed wall somewhere they could intersect. To avoid that you can simply, before placing visualization of future wall position, cast from place point raycast to 5 directions size of mesh to make sure there isn’t any other collider intersections, and shift accordingly or display that there no way to place wall. Keep in mind that this is only a clue of workaround since it may be achieved differently.

Hope that will help and give overall hints where to go further.