I am attempting to create RTS style “structure” placement on a surface and I need to check if the ground area below the structure is level. In order to do this, I am casting rays from 9 points around the structures ‘bounds’ and averaging the hit normals to ensure the ground can be considered level. In order to do that, I am having to create an array of 9 points like so:
Vector3 lpos = new Vector3(_tr.position.x - _groundMarker.renderer.bounds.extents.x, _tr.position.y, _tr.position.z);
Vector3 rpos = new Vector3(_tr.position.x + _groundMarker.renderer.bounds.extents.x, _tr.position.y, _tr.position.z);
Vector3 tlpos = new Vector3(_tr.position.x - _groundMarker.renderer.bounds.extents.x, _tr.position.y, _tr.position.z - _groundMarker.renderer.bounds.extents.z);
Vector3 trpos = new Vector3(_tr.position.x + _groundMarker.renderer.bounds.extents.x, _tr.position.y, _tr.position.z + _groundMarker.renderer.bounds.extents.z);
Vector3 blpos = new Vector3(_tr.position.x - _groundMarker.renderer.bounds.extents.x, _tr.position.y, _tr.position.z + _groundMarker.renderer.bounds.extents.z);
Vector3 brpos = new Vector3(_tr.position.x + _groundMarker.renderer.bounds.extents.x, _tr.position.y, _tr.position.z - _groundMarker.renderer.bounds.extents.z);
Vector3 tmpos = new Vector3(_groundMarker.renderer.bounds.center.x, _tr.position.y, _tr.position.z + _groundMarker.renderer.bounds.extents.z);
Vector3 bmpos = new Vector3(_groundMarker.renderer.bounds.center.x, _tr.position.y, _tr.position.z - _groundMarker.renderer.bounds.extents.z);
Vector3 cpos = _groundMarker.renderer.bounds.center;
I am having to update these vectors every time the ground marker changes scale (depending on the size of the structure) and it just strikes me as kind of messy. Is there a better way to do this without having to cast 9 individual rays? It’s be nice if I could simplify this code right down. Any suggestions?