c# - Unity3D Raycasting -
writing c# script allow player construct simple blocks. reason, strike raycast forward playercam. when ray hit object - collison world coords hit.point if instantiate constructing block on coordinates - created overlapping other objects. have change coords.
how can point lay shown in picture above? allow me calculate created blocks' coordinates
you can take raycasthit's point
property , move out along intersected face's normal block's extent (half width; if it's unit cube, 0.5f); this:
if (input.getmousebuttondown (0)) { ray ray = camera.main.screenpointtoray (input.mouseposition); raycasthit hit; if (physics.raycast (ray, out hit)) { instantiate (prefab, hit.point + hit.normal * blockextent, hit.transform.rotation); } }
this instantiate new block @ raycast's intersection point (you'd need calculate center of intersected face if wanted them align precisely), , inherit rotation of intersected gameobject.
note won't prevent new block spawning inside existing block; keep spawning inside block raycasted against.
Comments
Post a Comment