Game Development Community

dev|Pro Game Development Curriculum

Platforms...a simple start to a 3D side-scrolling platformer

by Bryan Sawler · 03/07/2011 (11:07 am) · 22 comments

Platforms...
A simple 3D side-scrolling platformer sample in a step-by-step format.

Step 1: Creating the project.
Go ahead and make a new project, using the Full template. Learning from my previous (Das Swarmy) resource I've decided since I'm keeping this fully script I'm not going to waste 5 minutes building. Current time is 10:35am.

Step 2: Setting the camera.
We're going to pull the camera back from the side. In game/scripts/server/gameCore at the bottom of the function GameCore::onClientEnterGame(%game, %client), just UNDER %game.preparePlayer(%client); we're going to add:
// Make the camera side-facing
%client.setCameraObject(%client.camera);
%client.camera.setOrbitObject(%client.player, "0 0 " @ -mDegToRad(90), 15, 25, 25, false, "0 2 1", true);
%client.setFirstPerson(false);

So we're aiming the camera at a -90 degree rotation, setting the camera 25 units away from the player, and actually aiming the camera 2 units in front of the player, and 1 unit up (we want to aim at the chest, not the feet).

Step 3: Controls
We're going to set REALLY simple controls. The right key will make me face (and move) to the right. The left key will make me face (and move) to the left. Mouse will do NOTHING, and up/down will (currently) do nothing. So, open up game/scripts/client/default.bind.cs and move down to the // Movement Keys section. Just under $movementSpeed = 1; we're going to add:
$facingDirection = 0; // right = 0, left = 1
A direction of 0 will be right, a direction of 1 will be left. Continuing down, the function moveleft and moveright will become:
function moveleft(%val)
{
	if(%val > 0 && $facingDirection == 0)
	{
		$mvYaw = mDegToRad(180);
		$facingDirection = 1;
	}
	$mvForwardAction = %val * $movementSpeed;
}
function moveright(%val)
{
	if(%val > 0 && $facingDirection == 1)
	{
		$mvYaw = mDegToRad(180);
		$facingDirection = 0;
	}
	$mvForwardAction = %val * $movementSpeed;
}
Now we're going to remove the lines in moveforward and movebackward so they become:
function moveforward(%val)
{
}

function movebackward(%val)
{
}
We'll leave the functions there and bound so that we can use them later. Lastly, we kill mouse turning / suppose so search for the line moveMap.bind( mouse, xaxis, yaw ); and comment it AND the yaxis line out. Save the file, and while you're in game/scripts/client delete config.cs and prefs.cs if they exist. The time is now 10:45

Step 4: Facing direction
We're going to make the camera bias to the direction we're facing. This will be a 2-step process so first, we need to setup a server command, and then we'll make the client call that comment. So, open up game/scripts/server/commands.cs and at the bottom of the file we'll add:
// Platformer support
function serverCmdsetCameraDirection(%client, %direction)
{
        %camera = %client.camera;
        %client.camera.setOrbitObject(%client.player, "0 0 " @ -mDegToRad(90), 15, 25, 25, false, %direction, true);
}
Now head back to that lovely game/scripts/client/default.bind.cs and we'll be adding the following:
Under $facingDirection we're going to add
$lookingOffset = 0;     // 0 = center, 1 = up, -1 = down
This will allow us to look up / down (told you we'd do something with them)
We'll be further modifying moveleft and moveright, to add a call to that serverCmd we just specified. So, edit them so they look like:
function moveleft(%val)
{
        if(%val > 0 && $facingDirection == 0)
        {
                $mvYaw = mDegToRad(180);
                $facingDirection = 1;
		commandToServer('setCameraDirection', "0 -3 " @ $lookingOffset); // ADDED
        }
        $mvForwardAction = %val * $movementSpeed;
}
function moveright(%val)
{
        if(%val > 0 && $facingDirection == 1)
        {
                $mvYaw = mDegToRad(180);
                $facingDirection = 0;
		commandToServer('setCameraDirection', "0 3 " @ $lookingOffset); // ADDED
        }
        $mvForwardAction = %val * $movementSpeed;
}
The added lines are noted there. This will tell the server when we've changed directions, and update the camera. Next up we're going to make moveforward and movebackward actually DO something. These will actually be our look up / look down functions (you can rename them if you like, which means also editing the moveMap.bind calls below them).
function moveforward(%val)
{
	if(%val > 0)
	{
		$lookingOffset = 1;
	}
	else
	{
		$lookingOffset = 0;
	}

	if($facingDirection == 0)
	{
		commandToServer('setCameraDirection', "0 3 " @ $lookingOffset); // ADDED
	}
	else
	{
		commandToServer('setCameraDirection', "0 -3 " @ $lookingOffset); // ADDED
	}
}

function movebackward(%val)
{
	if(%val > 0)
	{
		$lookingOffset = -1;
	}
	else
	{
		$lookingOffset = 0;
	}

	if($facingDirection == 0)
	{
		commandToServer('setCameraDirection', "0 3 " @ $lookingOffset); // ADDED
	}
	else
	{
		commandToServer('setCameraDirection', "0 -3 " @ $lookingOffset); // ADDED
	}
	doCrouch(%val);
}
Now we can look up/down as we go. Down will ALSO make us crouch

So here we go. It's now 11am. We have the VERY simple start of a platformer game. If there are specific requests for additions to this, please just ask, or if there are any other game "starting points" (similar to this / the 8-directional shooter Das Swarmy I posted) please ask! I have a few ideas for a part 2 if people seem interested.

About the author

President and Founder of The Muteki Corporation Makers of such fine iPhone games as MazeFinger, Topple 2, and The Battle of Pirate Bay!

Page«First 1 2 Next»
#21
07/13/2011 (3:14 pm)
@Bryan, Great resource but I was wondering if you could add the support for vehicles.

Also I have noticed that went you die and respawn the camera is messed up.
#22
02/06/2013 (4:13 pm)
To add to this, if you respawn you have to move before the camera is reset to fix that I added the camera code to the end of GameCore::spawnPlayer after
if (!isDefined("%noControl"))
      %client.setControlObject(%control);

Now I just need to tweak the death camera.
~Ark
Page«First 1 2 Next»