Game Development Reference
In-Depth Information
Step 10: Repeat for the Hallway_Key.
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);
}
if (hit.collider.gameObject.name == “Hallway
_Key”){
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?
This is just doing the same thing as the power switch. Notice that the
script lists this next if statement after the power switch comparison.
Essentially, Unity simply looks to see if the object is called “Hallway_
PowerPanel_Switch” and if it isn't, it moves down to the next set of
instructions. It then checks to see if the object its ray is casting on is called
“Hallway_Key” and if it isn't, it does nothing.
Now, these two if statements look an awfully lot alike. And in fact, it could
be simplified into if(hit.collider.gameObject.name == “Hallway_
PowerPanel_Switch || hit.collider.gameObject.name ==
“Hallway_Key”{ . However, we want to make sure that there are actually two
very different things that happen if these objects are clicked. So separating
them into different commands allows for an easy way to distinguish that.
 
Search WWH ::




Custom Search