Infinite Roller Tutorial Four – Following the Character and Jumping

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, we made the terrain infinite and started writing a character controller. Today, we’ll make the camera follow the character and add in the functionality to make it jump.

Load up your InfiniteRoller project and open the scene, then create a new C# script in the Scripts folder (in the project window, right click, Create -> C# Script), and name it CameraFollow. Open it and delete the code inside the class. Next, create a new variable

//the object the camera should follow
public Transform followTarget;

The target will be the character. It’s public because we want to assign it in the inspector. Transform is the class name of the transform we’ve used before – it holds information about a gameobject’s position, rotation and scale in the scene. Save the script and go back to Unity. Click on the camera and drag the script onto the inspector to add it as a component. Next, drag the character from the hierarchy to the followTarget slot.

Go back to the script and add

private Vector3 newCameraPosition;

And an Update method.

void Update()
{
	//get the new position of the camera and follow the target when it moves (x and y only – z stays the same)
	newCameraPosition = new Vector3(followTarget.position.x, followTarget.position.y, transform.position.z);
	transform.position = newCameraPosition;
}

So we’re assigning a new value to the camera’s position in every frame. The x and y values of the new position are the character’s x and y positions. As this is a 2D game, it won’t move in the z direction, and we want the camera to remain at it’s current distance, so we use the camera’s transform to set the z position. The code then sets the camera’s position to the new position we just calculated. I’ve used a private class variable for newCameraPosition rather than creating a new Vector3 object inside the Update method because this way, a new variable isn’t created on each frame. It’s something you don’t have to do, but if your game if running too slowly, this is one of the things you might want to look at doing in your code. I doubt it will make much difference for this game though. Alternatively, you could condense this into one line, writing transform.position = new Vector3(followTarget.position.x, followTarget.position.y, transform.position.z);

Unity tutorial follow character

Save the script and go back to Unity. Press play. You should see the camera is now following the character in the game window.

Next, we’ll make the character jump. Open the RollingCharacterController script and create a new variable

//set the force of the jump
public int jumpUpForce = 450;

We’re going to add an upward force to the character’s rigidbody to make it jump. It’s public so that we can change it in the inspector. Add another variable

//check if we've jumped
private bool jump;

This bool will be set to true if we press a button we assign to make the character jump. We can make this work across different platforms (desktop/android etc) by importing Unity’s CrossPlatformInput asset package. To do this, go back to Unity and right click in the project window. Go to Import Package -> CrossPlatformInput, then click Import when the import window pops up. This gives us access to some extra scripts.

Go back to the script and add an Update method:

void Update()
{
	jump = CrossPlatformInputManager.GetButtonDown("Jump");
}

We use GetButtonDown(“Jump”) to get the jump button assigned to whichever platform we are targeting. CrossPlatformInputManager lets us target multiple platforms at once – if it’s on my computer, jump is space bar. If it’s a touch screen device, jump is tapping the screen. GetButtonDown returns true when the button is pressed.

In the FixedUpdate method, after the line setting the velocity, add

if (jump)
{
	rigidbody2D.AddForce(new Vector2(0, jumpUpForce));
	jump = false;
}

If we’ve pressed the jump button, jump is true, and we add an upward force to the rigidbody. AddForce takes a new Vector2. We don’t want to add a force in the x direction, so that is 0. We add a value of jumpUpForce in the y direction. We then set jump to be false because FixedUpdate() can run more often than Update(). If we didn’t add in this line, it would mean jump would stay true, we would go into the code above again, and add another force until Update() was called and jump was set back to false. Since we only want to add the force once each time we press the jump button, we set jump to be false.

Unity tutorial 2d character jump controller

Save the script and go back to Unity.

Check the console window for any error messages. At this point, my console had a warning message in it (see the image below) saying “’RollingCharacterController.rigidbody2D’ hides inherited member ‘UnityEngine.Component.rigidbody2D’. Use the new keyword if hiding was intended”. This is an obsolete message and you can ignore it in this case. In older versions of Unity, rigidbody2D (and other variables like rigidbody) were exposed variables in the MonoBehavior class (the class that our RollingCharacterController class is a subclass of) which meant you could just type rigidbody2d into the subclass, and behind the scenes, Unity would do GetComponent(). Now we have to use GetComponent() explicitly.

Unity tutorial warning message

Press play and test that the character jumps when you press space.

Save the scene and the project.

Today we created a script to make the camera follow the character, and added the ability to jump. Next time, we’ll make the terrain more interesting by adding gaps that the character would need to jump over, and we’ll add character death when it falls through the gaps.

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

PreviousTutorialButtonNextTutorialButton

2 Comments:

  1. Saved as a favorite, I really like your web site!

Leave a Reply

Your email address will not be published. Required fields are marked *