Game Development Reference
In-Depth Information
Why?
We don't want Unity to be chugging away trying to move an object that isn't
there yet. The if statement there ( if (empPlacement != null){) makes
sure that empPlacement has indeed been populated. If it has not, it doesn't
fire the two lines beneath it that move it. But if it has been populated (if its
value is not null), then go ahead and set empPlacement's position to match
the point the ray hits (hit.point) and rotate in the direction of hitDir.
Step 14: Actually place the “real” EMP:
if (empActive){
if (!empGhostActive){
placingEMP = Instantiate(empGhost, hit.point,
hitDir);
empGhostActive = true;
}
if (placingEMP != null){
placingEMP.transform.position = hit.point;
placingEMP.transform.rotation = hitDir;
}
if (Input.GetMouseButtonDown(0)){
Destroy (placingEMP);
placedEMP = Instantiate(empReal, hit.point,
hitDir);
ExplodeEMP(placedEMP);
}
}
Why?
This last block of code says, “if the user presses the mouse button (if
( Input.GetMouseButtonDown(0)){ ), then destroy the empPlacement
object (which is the instantiated ghost version of the EMP), and instantiate
the object empReal at the point and rotate it to the surface's normal. Then,
run the function ExplodeEMP (that we haven't created yet). When you run
ExplodeEMP pass to this function the object that we just used to populate
placedEMP.
This is another instance of the importance of understanding scope. We
defined placedEMP with the line placedEMP = Instantiate(empReal,
hit.point, hitDir);. But because this definition is within an if
statement block and inside an Update function, this would not carry on
down to the ExplodeEMP function (that we're going to write in a minute).
This is why we have to manually pass down to the ExplodeEMP the
parameter of what placedEMP is.
Step 15: Clean up. We're going to add one more block of code that may
seem a little obscure right now but will be important later. This will make
sure there aren't extra ghost EMPs floating around:
Search WWH ::




Custom Search