Game Development Reference
In-Depth Information
that say, “if you Raycast onto the power switch, highlight it (using the code in
the AddHighlight function) and then if the mouse is clicked, turn the lights
on.” Then, later we can say, “if you Raycast on the key, highlight it (using
the code in the AddHighlight function) and then if the mouse is clicked
pick the key up.” And so on. In each case we're using the exact same
highlight mechanism, but not having to retype all those instructions.
Step 9: Check to see if the object being hit by the ray has the same name
as “Hallway_PowerPanel_Switch,” and if it does, call up the AddHighlight
function:
function Update (){
var hit : RaycastHit;
if (Physics.Raycast (transform.position, transform
.forward, hit, 100))}
Debug.DrawLine (transform.position, hit
.point);
Debug.Log (“Selected object is ” + hit
.collider.gameObject.name);
if (hit.collider.gameObject.name ==
“Hallway_PowerPanel_Switch”){
AddHighlight(hit.collider.gameObject);
}
}
}
function AddHighlight(hiOb:GameObject){
hiOb.renderer.material.color.r = hiOb.renderer
.material.color.r*10;
hiOb.renderer.material.color.g = hiOb.renderer
.material.color.g*10;
hiOb.renderer.material.color.b = hiOb.renderer
.material.color.b*10;
yield WaitForSeconds (0.1);
hiOb.renderer.material.color.r = hiOb.renderer
.material.color.r/10;
hiOb.renderer.material.color.g = hiOb.renderer
.material.color.g/10;
hiOb.renderer.material.color.b = hiOb.renderer
.material.color.b/10;
}
Why?
So what's happening there is that (every frame) the ray is cast, the two
debug lines are still happening, and then it compares the name of the
object hit to “Hallway_PowerPanel_Switch” and if the names do match,
it calls up the AddHighlight function and passes to it the very object it is
casting on (which is the switch). The AddHighlight receives the message
to fire, and upon which object to fire, and does its thing (turns up the RGB
values; then turns them down again).
Search WWH ::




Custom Search