Game Development Reference
In-Depth Information
Why?
This all provides a crosshairs that sits exactly in the middle of the screen. This
also happens to be the location of where the ray that will be generated by
the raycasting will be terminating. Later we'll be locking the mouse down
and hiding it so the player can't see it unless he is picking a weapon/tool
from the inventory. So giving the player a location for the crosshairs allows
him to have some idea of where the computer is looking for input from.
By making the texture a bit transparent, we can try and allow the
crosshairs to be a good visible hint, but not become overly important in
the overall visual impact of the game. In fact, if you still find the crosshairs
to be too dominant, turn the Alpha down further.
Step 3: Create a new JavaScript. Rename it AC_ToolFunctionality
Script. Open the script.
Step 4: Within the extant function Update, add the following:
function Update () {
var hit : RaycastHit;
if (Physics.Raycast (transform.position, transform
.forward, hit, 100)){
Debug.DrawLine (transform.position, hit
.point);
Debug.Log (hit.collider.gameObject.name);
}
}
Why?
This is one of the first times we've used the function Update. We're
telling Unity to do something every frame: first, create a variable to store
the object that the ray hits (RaycastHit); then, check if there is indeed
anything that the ray strikes, and specifically, that the ray that emerges
from the position of the object this script is attached to (it'll be attached
to the camera) shoots straight forward for 100 units; and finally (for
illustration purposes) we're including a couple of Debug functions there.
The first one actually draws the ray the raycasting mechanism is using
(Debug.DrawLine) and the second is writing the name of the object that
the ray strikes to the Console (Debug.Log).
Step 5: Save and return to Unity. Fix any syntax errors that pop up in the
console.
Step 6: Attach this script to the Main Camera that is a child of the First
Person Controller (add a First Person Controller if needed). Go ahead and
click OK if Unity warns of breaking the prefab.
Step 7: Play the game. Take special note of the Scene view and the Console.
Look to see the bright white line emanating out of the camera that is the
Debug.DrawLine and check out the Console to see how it is returning the
name of the objects the ray comes in contact with ( Figure 14.6 ).
Search WWH ::




Custom Search