Generate flying cubes

Hello guys and gals,

Im currently creating a 2d run and jump game, and im up to the stage where im in need of obstacles, and i was hoping i could do this with somehow spawning cubes.

I’ve looked into it, and unfortunately ive had no luck what so ever…

heres what i have so far:

The player only moves up and down. So im hoping there’s a way to make the cubes that spawn travel along the X axis.

Thanks to any one that could help me out

Step one is to create the cubes, step two is to make them move. So I’d do something like this.

On any game object-

var cube : Transform;
var spawnPoint : Transform;
var spawnRate : float;

function Update () {

      StartCoroutine("Spawn", 0);
}

function Spawn () {

      yield WaitForSeconds(spawnRate);
      Instantiate(cube, spawnPoint.transform.position, spawnPoint.transform.rotation);
}

On your cube prefab-

var speed : float;

function FixedUpdate () {

      rigidbody2D.velocity.x = -speed;
}

Remember to have a Rigidbody2D component on your cube prefab.

I didn’t test this so I may have overlooked something, but this or something similar should do what you want.

Thanks for this! Ill try it out when i get home from work. :slight_smile: