Game Development Reference
In-Depth Information
if (empActive){
if (!empGhostActive){
placingEMP = Instantiate(empGhost, hit.point,
hitDir);
empGhostActive = true;
}
}
Tips and Tricks
As you've probably gathered by now ! means “not.” So (!empGhostActive)
means empGhostActive is not true. Or != means “not equal to.”
Why?
Some important things are happening here. This collection of code is
within a function Update. This means that the code is firing every frame.
If we were to just use empPlacement = Instantiate(empGhost,
hit.point, hitDir); without checking to see if empGhostActive was
not true (!empGhostActive), Unity would be instantiating empGhost
every frame and leaving a trail of ghosted EMP mines.
What this block does is create the empGhost object and assign it to the variable
empPlacement, and then immediately sets the empGhostActive state to true
( empGhostActive = true; )—all in the same frame. So in the next frame,
since empGhostActive is true, it doesn't fire the code to Instantiate again.
The problem with the code right now is that this creates the empGhost
(and has this newly instantiated object populate the empPlacement
variable), but this new ghost is sitting right where it was created. We
need it to move around according to where the player looks.
Step 13: In the code check to see if empPlacement has been populated,
and if it has, have empPlacement's position and rotation match the raycast
hit's location and normal direction:
if (empActive){
if (!empGhostActive){
placingEMP = Instantiate(empGhost, hit.point,
hitDir);
empGhostActive = true;
}
if (empPlacement != null){
placingEMP.transform.position = hit.point;
placingEMP.transform.rotation = hitDir;
}
Search WWH ::




Custom Search