Game Development Reference
In-Depth Information
If this script is attached to an object, it responds to OnRaycastHit by creating a hole at the
position of the ray hit. First step is to instantiate the prefab provided through holePrefab and
position it at hit. point , which is the position of the hit in world coordinates. If the hit object
has a rigid body attached to it, then there is a possiblity that the object moves or rotates at
any moment in the future. Therefore, it is necessary in this case to add the hole as a child of
the hit object, so that the object and the hole displace and rotate as one unit. An important
question to answer is: how must the hole be directed? The obvious answer is outwards. The
variable hit.normal returns a vector perpendicular to the surface that has been hit, where the
direction of this vector points outside from the object. By adding this vector to the position
of the hit, we get the correct position and direction of the hole. However, the quad object
in Unity has only one rendered face, which is the face that looks at the negative direction
of the local z axis. Therefore, the positive z axis of the quad must look towards inside like
in Illustration 63, in order to have the rendered face visible. Consequently, we set hit.point -
hit.normal as look point for the quad.
Illustration 63: Hole object made of quad, the positive z axis is looks inside so that the
rendered face looks outside
After rotating the hole correctly, we need to make sure that it is on top of the surface. This
guarantees that the hole is always in front of the object and hence visible. However, the
distance must be very small, otherwise the space between the surface and the hole becomes
Search WWH ::




Custom Search