Infinite Roller Tutorial Three – Infinite Terrain and Character Controller

Welcome to Edge of Code’s series of Infinite Roller tutorials. If you’d prefer a video tutorial, you can find one here.

In the previous tutorial, I explained what prefabs are and how to use them, and we wrote a simple script to generate some terrain. In this tutorial, we’ll start writing a character controller and make the terrain infinite.

Load up your InfiniteRoller project and open the scene. Create a new script inside the Scripts folder called RollingCharacterController and open it.

Add a public variable to set the speed at which the character will move

//set the speed it's moving forwards.
public int forwardSpeed = 8;

Let it be public so we can change the value in the inspector. Add a private variable

private Rigidbody2D rigidBody2D;

And add an Awake method that assigns the Rigidbody2D component of the gameobject to the rigidbody2D variable.

void Awake()
{
	rigidBody2D = GetComponent<Rigidbody2D>();
}

Add a new method

void FixedUpdate()
{
}

FixedUpdate is one of Unity’s own methods. It should be used when you want to update the physics of gameobjects with a rigidbody. The physics of gameobjects with a rigidbody are controlled by Unity’s physics engine, and the FixedUpdate method runs in sync with the physics engine. Unity has another method called Update() which is used to update things that are not related to physics. Inside FixedUpdate(), add

rigidbody2D.velocity = new Vector2(forwardSpeed, rigidbody2D.velocity.y);

to apply a constant velocity in the x direction.

Unity tutorial character controller

Go back to Unity and select the character in the hierarchy. Drag the script to the inspector to add it as a component, then press play. The character should roll to the right.

Open your TerrainGenerator script. We’re now going to make the terrain infinite by adding rules that remove the previousChunk and add a new one after nextChunk as the character rolls along. Add a new method:

void Update()
{
}

In Update(), we need to check if the character has reached a specific x position. If they have, we need to reassign which chunk is which. We’ll need a reference to the character, so create a new class variable

//the character
public GameObject character;

Then save, go back to Unity, click on the TerrainGenerator gameobject, and drag the character gameobject from the hierarchy onto the character slot in the inspector. Go back to the TerrainGenerator script and create new method

/** Check if the character has reached the next chunk*/
private bool PlayerReachedNextChunk()
{
	bool playerReachedNextChunk = false;
		
	return playerReachedNextChunk;
}

The return type in this case is bool, which can be true or false. The return statement is how we give back the bool information to the method that is calling this one. At the moment, this method will always return false. Add the following line in after the creation of the playerReachedNextChunk variable

float distance = player.transform.position.x – nextChunk.transform.position.x;

We’re calculating the distance between the character and the next chunk. We only need to consider the x direction as that’s the direction the character is moving in. After that line, add

if (distance > 0)
{
	playerReachedNextChunk = true;
}

This is an if statement. If the statement distance > 0 is true, we go into the code inside the curly brackets. So if the character has crossed onto the nextChunk (and therefore distance > 0), we set the bool to be true. If it’s not true, the playerReachedNextChunk variable stays false. Call this method in Update() – add

if (PlayerReachedNextChunk())
{
}

This if statement tests whether PlayerReachedNextChunk() returns a value of true or false. If it is true, we go into the code within the curly brackets, which we’ll add next. Create another new method,

** reassign currentChunk to previousChunk, nextChunk to currentChunk*/
private void ReassignChunks()
{
	//keep a reference to the previous chunk		
	Chunk refToPreviousChunk = previousChunk;
	
	//reassign the chunks so that we reuse the old one
	previousChunk = currentChunk;
	currentChunk = nextChunk;
	nextChunk = refToPreviousChunk;
	
	//nextChunk is now what was the previousChunk, so move it over to the appropriate position
	float xPosition = nextChunk.transform.position.x + 3*nextChunk.WidthGroundPiece*Chunk.CHUNK_SIZE;
	nextChunk.RepositionChunk(new Vector3(xPosition, 0, 0));
}

We keep a reference to the previous chunk, then move each chunk over to the next one and reassign nextChunk to the reference. Next, re-position nextChunk to the right by 3 chunks (since it’s in the position previousChunk was). Call this method from inside the if statement in Update():

if (PlayerReachedNextChunk())
{
	 ReassignChunks();
}

This is all we want to do at the moment.

Unity tutorial infinite terrain generation

This should generate infinite, flat, boring terrain – the character will just keep rolling. Save the script, then go back to Unity and press play. You should be able to see how the terrain is being generated by pressing and holding the middle mouse button, and dragging across the Scene view.

Unity tutorial infinite terrain

Save the scene and the project.

In this tutorial, we’ve started coding a character controller, and we’ve added code to make the terrain infinite. Next time, we’ll make the camera follow the character, and the character jump.

Remember you can download the files for this tutorial on the Downloads page. See you next time!

PreviousTutorialButtonNextTutorialButton

Comments are closed.