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?
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.
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);
}
}
}
`