Game Development Reference
In-Depth Information
Direction
Origin
The Ray struct contains the origin point as a Vector3 and the direction as a Vector3.
In the Unity editor Project panel, open the Assets ➤ Sample Assets ➤ Sample Scenes ➤ Scenes
folder and open the Third Person Character scene. In the top menu select File ➤ Save Scene as . . .
and save the scene as RaycastTest.
Again in the Project folder, select the Assets ➤ Scripts folder, then select Create ➤ Javascript,
name the new script RaycastFun, and open it in MonoDevelop. In normal gameplay you aren't going
to see the rays, but Debug.DrawRay(origin, direction, color) is a great help during development.
Edit the code to the following:
public var rayLength : float = 2;
function Update () {
Debug.DrawRay(transform.position + Vector3(0, 0.5, 0), transform.forward * rayLength, Color.white);
}
Breaking this down:
(1) public var rayLength : float = 2;
Declare the float variable rayLength as public to make it accessible from the Inspector.
(2) Debug.DrawRay(transform.position + Vector3(0, 0.5, 0),
transform.forward * rayLength, Color.white);
Uses the game object's transform.position as the origin of the ray, then adds 0.5 to the y
coordinate to raise it from the floor a little. The ray's direction is where the player character is
facing, so this is the game object's local transform.forward , with the length adjusted by the
factor rayLength . You can change the color of the ray for optimal visibility. There are a few more
parameters we won't be using, such as the time the ray is visible and how it is drawn relative to other
game objects. +' on DrawRay for the full Scripting Reference.
 
 
Search WWH ::




Custom Search