We are going to create obstacles for the ninja to avoid. These obstacles need to repeat until the player dies, and they need to spawn randomly to provide challenges, variety, and replay ability.
Since the player is static, the obstacles need to move towards the player. To create our obstacle, we can execute the following steps:
Add a folder to
Assets
within the Project Browser namedObstacle
.Download and unzip the image
Boulder.png
from http://freeartsprites.com/free-art/endless-runner/.Create a sprite GameObject using the following image:
Position the boulder obstacle as in the following screenshot:
Go to Add component | Physics 2D | Box2D Collider.
Go to Add component | Physics 2D | Rigidbody 2D.
Set Gravity to
0
.Create a script called
Obstacle.js
, and assign it to theObstacle
GameObject.
Add the following to the script:
Note
Declare our obstacle
variable and set its data type to Rigidbody2D
. This is shown in the next line of code.
var...