- Home /
Mario like 2D Camera
Hi! i am making an 8 bit platformer game and I wanted to know if there is a way to make a camera that only follows the player on the x or Horizontal Axis. I want it to be like the camera in the 2D Mario games where it doesn't follow the y axis. could somebody tell me if there is a way to do this?
Answer by Jessespike · Jun 23, 2016 at 05:15 PM
To center the player in camera, by following the player's x-position, while ignoring the y and z positions.
public Transform player;
// Update is called once per frame
void Update () {
transform.position = new Vector3(player.position.x, transform.position.y, transform.position.z);
}
Features to consider might be preventing the camera from moving off the end of the level or edge of the map, smoothing the camera's movement, or move the camera ahead of the player so they can see further in the player's direction.
Answer by proefra27 · Feb 18, 2018 at 05:01 PM
public class SigueMario : MonoBehaviour {
private Transform player, Izq, Der;
void Start () {
player = GameObject.Find ("Mario").transform;
Izq = GameObject.Find ("Izquierda").transform;
Der = GameObject.Find ("Derecha").transform;
}
void Update () {
Vector3 playerpos = player.position;
playerpos.z = transform.position.z;
playerpos.y = transform.position.y;
if (playerpos.x < Izq.position.x) {
float dif=Izq.position.x-playerpos.x;
transform.position = new Vector3(transform.position.x-dif,transform.position.y,transform.position.z);
}
if (playerpos.x > Der.position.x) {
float dif=Der.position.x-playerpos.x;
transform.position = new Vector3(transform.position.x-dif,transform.position.y,transform.position.z);
}
}
}
`
Your answer
![](https://koobas.hobune.stream/wayback/20220612083309im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do I get my 2D game camera to view the respawned sprite? 1 Answer
Help with a 2d camera Controller 1 Answer
Advice on implementing metroidvania camera 1 Answer
Trying to make the camera follow the player but stop at the edge. 1 Answer
How to make a companion that trails character moves, and not collide? 0 Answers