Game Development Reference
In-Depth Information
If you run the course now, your character can't make it across the gap. Impossible obstacles in an
unwinnable game are no fun. To address this gap, in the Project panel's Assets ➤ Sample Assets ➤
Prototyping ➤ Prefabs folder find the Platform 4 × 1 × 4 prefab, and drag and drop it into the Scene
view. Give it a Transform Position of (0,12, 38). This places it in the middle of the gap, slightly higher
than the elevated track. Playtest now, and you can guide the player character across the gap using
the spacebar to jump to the platform and off again on the far side back to the elevated track.
A helpful stepping stone is necessary to fill the gap for the player to be able to continue onward,
but it can be a more challenging obstacle by moving up and down. In the Project panel's
Assets ➤ Scripts folder, create a new script named MovePlatform. Open it in MonoDevelop and edit
the code as follows:
#pragma strict
private var platformStartPosition : Vector3;
private var platformEndPosition : Vector3;
public var lowLevel : float = 6;
public var duration : float = 5.0f;
private var step : float = 0.0f;
private var movingBack : boolean = false;
function Start () {
platformStartPosition = transform.position;
platformEndPosition = (transform.position - Vector3(0, lowLevel, 0));
}
function FixedUpdate() {
var delta : float = (Time.deltaTime / duration);
if(movingBack) step -= delta;
else step += delta;
step = Mathf.Clamp01(step);
rigidbody.MovePosition(Vector3.Lerp(platformStartPosition, platformEndPosition, step));
if(step == 1.0f) movingBack = true;
else if(step == 0.0f) movingBack = false;
}
There are many ways you could have written this script. Essentially you want the platform to move
from point A to point B and back again. However, you also want to take into account other factors
such as the reusability of a script and ease of testing/tweaking behavior.
With this in mind, this script breaks down as follows:
(1) private var platformStartPosition : Vector3;
...
function Start () {
platformStartPosition = transform.position;
...
 
Search WWH ::




Custom Search