0
\$\begingroup\$

I'm basically bumming around using Unity's 2D creation tools, and I've managed to create a little platformer physics demo. There's a lot of work to be done, but I've got basic movement down, some sounds working, basic jumping physics, and whatnot.

One of the things that's on my list of things to tackle is the camera system.

I experimented with locking the camera to the player position; it's simple, but works. However, I'd like to attempt to develop a more complex camera system that would follow level constraints, but still allow for free vertical and horizontal movement. Currently, I have the camera only matching the player's movement on the X axis, which means that jumping doesn't affect the camera. However, the camera won't follow if you climb a tall ladder.

(Think of the viewport from the Megaman X SNES games; there are many vertical and horizontal segments, but the camera is smart enough to let the player character (X) jump or cling to walls at the edge of the viewport without moving, yet move when the player is in an open area. This is the type of camera I'd like to achieve.)

I'd like to know how this problem is commonly approached. I thought about creating a spline, perhaps, and having the camera follow the spline path while still tracking the player. This spline would be specific to each created level. However, I'm not sure if this is the correct solution (and I'm not sure how it would work in open areas). The other thing that might be possible could be to set bounds for the actual camera limits, and not allow the camera to move outside of that, but I feel as though that might be a brittle solution in tighter areas.

Could someone with experience point me to any resources, or suggest advice to go in the right direction? Thanks!

Edit: To make this more clear, I've included a link to Flame Mammoth's stage in Megaman X. Notice how, in the first part of the stage, X can dash along the ground, while the camera tracks his x-position; later, it tracks his y-position when he falls down a shaft. Further on, there's a large room where the camera tracks X's movement without any boundaries until he gets close to the top of the screen; the camera doesn't pan past those boundaries.

Of note, I found a solution here that helps with jumping; I believe that MMX uses a variant of this. However, notice that in the custom game at the bottom, the camera is allowed to pan "out of bounds" as it's not limited by the level.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Just go really simple. Add a flag to the player that tells the camera when they're jumping. If they're jumping, don't follow them up.

The other situation you need to handle is when the player jumps up or down to different levels. In this case it would be pretty simple to start tracking again when the player touches down, or if the player goes below the starting position of the jump, the camera will start following again.

For keeping the camera in bounds, you have a few different options.

  • Create a bounds object that the camera tests before moving to ensure that it doesn't move outside. This is a quick and easy thing to implement, but only deals with the camera position, and not the view frustum. This means you might have to do a lot of tweaking to the bounds to make sure it doesn't see anything outside the bounds, and gets tricky-er if you have any kind of zoom. To implement this, you can create a number of objects with bounds, add them all to the same layer, something like CameraBounds, then, when testing a new position before moving there, do something like a physics overlap sphere (passing in the CameraBounds layer mask) to make sure the new position is inside one of your bounds objects.
  • Unity has a method called OnBecameVisible. You can place objects near the outside edges of your level. When these become visible to the camera, they tell the camera to stop tracking in that direction. This'll take some tweaking to get a smooth feel, but would ensure that no matter the zoom or camera position, you wouldn't see anything outside the level.
  • Finally, you have a predefined path. This is a path that the camera will "gravitate towards". The camera will also follow the character, but the farther it gets from the predefined path, the less effect the character's position has on the camera position. This will likely give you the best results, but is also probably the most work.
\$\endgroup\$
4
  • \$\begingroup\$ Sorry, I might not have been clear with my question. This addresses the jumping aspect (especially the different levels; thinking about it, I think that's how it's done in MMX, so thank you for that!), but I'm more concerned about the camera clipping incorrectly. Somehow, camera bounds are set so that you can't see outside the level in a 2D platformer. This behavior overrides the "keep the player in the middle of the screen" rule. Does that make sense? \$\endgroup\$
    – user6662
    Commented Jul 14, 2014 at 17:04
  • \$\begingroup\$ Yep, I'll update. \$\endgroup\$
    – House
    Commented Jul 14, 2014 at 17:05
  • \$\begingroup\$ I added a bit to my question to hopefully make it more clear. I realize that I'm not really explaining it very well - it'd be easier if I had a whiteboard or something. :P \$\endgroup\$
    – user6662
    Commented Jul 14, 2014 at 17:32
  • \$\begingroup\$ Whoa, this is awesome! Thanks, dude. The third option is what I was thinking with the spline idea - I also thought that it might be possible to use multiple cameras and switch between them in different areas. However, I'll definitely try the first option and see what mileage I can get out of it. \$\endgroup\$
    – user6662
    Commented Jul 14, 2014 at 17:40

You must log in to answer this question.