Infinite Platform Generation (Part 2)

5
The Camera moves along with Pixi and platforms are generated ahead of camera view

 

  1. Add a box and change the x scale to 7, duplicate it and place it a little apart from the previous one.
  2. We need a point which is moving along with our player Pixi, from our last tutorial the Part 1 we know that camera is following Pixi. So it’s a good choice to add a point (an empty game object) as a child of the camera. Rename this empty game object as Platform Generation Point, it will serve as a reference point for spawning our platforms. Make sure that you place the Platform Generation Point far from the camera view.

6

3. We have set up a reference point to guide our spawning but where to exactly spawn our platform? Let’s create an empty game object named Platform Generator and place it at the center of the second platform.

4. Now we need to make a C# script, name it as PlatformGenerator. This script will see if the x co-ordinate of Platform Generator is less than the Platform Generation Point then we need to add more platforms.

5. The location of the next platform will be incremented by the width of the platform and the distance between them that we want to keep.

public GameObject thePlatform;
public Transform generationPoint;
public float distanceBetween;

private float platformWidth;

void Start () {
platformWidth = thePlatform.GetComponent<BoxCollider2D> ().size.x;

}

void Update () {
if (transform.position.x < generationPoint.position.x)
{
transform.position = new Vector3 (transform.position.x + platformWidth + distanceBetween, transform.position.y, transform.position.z);
Instantiate (thePlatform, transform.position, transform.rotation);
}

}

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s