Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

2D Camera for Platformer

Discussion in '2D' started by FatalitySock, Jun 28, 2015.

  1. FatalitySock

    FatalitySock

    Joined:
    Apr 25, 2015
    Posts:
    3
    Hello everyone,

    I'm currently working on an 2D Platformer called Phil's Adventure. For that game I build a slightly more complicated camera and I would like to share my ideas with you and help you build your own similar 2D camera.

    My 2D camera is platform-snapping and has a threshold triggered dual-forward-focus.

    The result looks like this.

    The reason for a bit more complicated version of a 2D camera is that I want:
    1. The player to see more of what is coming
    2. Not having the camera moving constantly around, for example while fighting an enemy or jumping

    You can see several borders. They all define thresholds visually and can be adjusted in unity without additional coding.

    First let's discuss the Y-Axis, since this was the easier part.

    Y-Axis

    If the player crosses the bottom black border, the camera should follow the player since we don't want the player to jump out of the screen if he falls.

    code part 1

    Additionally we want the camera to move towards the player if the player is standing on the ground or on a platform.

    I set a tolerance for the camera that is larger than a tile height. That means the camera follows only if the player is landing on a platform at least two tiles higher than his old position and doesn't change if the player is landing on a block just one tile height higher.

    code part 2

    Now for the
    X-Axis


    On the one hand I wanted the camera to have a forward focus and when the player is changing directions, the camera should change its focus backwards. This should happen smoothly.
    On the other hand, if the player is moving in one direction, the camera should follow the player instantly.

    I solved this with a small threshold "xThreshold". If the player is moving right and is close to the left border (within the threshold) the camera should follow instantly. If the player is further away on the right side of the left border, it means the focus changed a short while ago and the camera should move smoothly with constant speed. The solution for moving left is of course similar.

    For changing the focus direction I used again a threshold, but this time defined by the far left and right borders, which you can see in the example above.

    code part 3

    and you can finde the full script here and as an attached file.

    I hope it helps some of you! (^_^)v
    If you have any suggestions or if you found any error (even spelling or grammar errors) let me now!

    FatalitySock (@IndieFlorianG)

    PS:I uploaded the code on a different site just for better formatting support. Sorry for the inconvenience.
     

    Attached Files:

  2. skinwalker

    skinwalker

    Joined:
    Apr 10, 2015
    Posts:
    505
    Thank you man, it works like a magic !

    Actually I am not working on a platformer game, but a runner, so I had to change it a little and it works great, I was looking for something like this for a long time xD

    I love the smooth Y movement.
     
  3. FatalitySock

    FatalitySock

    Joined:
    Apr 25, 2015
    Posts:
    3
    Thank you!
    I'm glad I could help you out and I hope to see how you applied the camera in your game soon.

    Good luck!
     
  4. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    I'm using a similar approach, but not quite the same. I'm confining the player to a box. Whenever they move left or right out of the box, then the camera is dragged with them. In addition, the camera also moves so as to provide "look ahead" space.

    I haven't looked at your code - how do you handle moving the camera over to the other side while the player is moving in that direction at the same time? For example, the player has been walking to the left, and the camera had moved to provide "look ahead" space to the left. Now, the player is moving to the right and moving over the threshold line, which means the camera has to move all the way to the right to provide "look ahead" space there. Meanwhile, the player keeps walking to the right while the camera is moving, which means the camera must move even further to accomodate for that. I've found that smooth camera movement doesn't seem to be possible in this case using easing functions, so I've switched to simple linear movement. This makes it simple to set a new target X position for the camera and just keep it moving.
     
  5. FatalitySock

    FatalitySock

    Joined:
    Apr 25, 2015
    Posts:
    3
    You are right and I did basically the same.

    I used
    Code (CSharp):
    1. transform.position = Vector3.MoveTowards (currentPos, targetPos, time);
    to move the camera in the x-axis, which is a simple method to let the camera catch up with the player.