How do I lock Y Axis movement for Parented Camera?

Hi, I am working on a 2D platformer and I am trying to emulate the camera movement of the original Super Mario games. The camera follows your player horizontally along the X Axis but does not travel up on the Y axis when you jump. This video shows what I mean:

Right now I have my main camera parented to my player game object. If I jump, the camera travels up on the y axis to follow the character. I would like it so that the camera follows my player horizontally as I run left or right on the x axis, but does not raise up every time I jump.

What script could I try to keep the camera y position locked but still allow it to scroll along the x axis?

Any help would be appreciated -- thanks!


Personally, I would suggest writing camera code at some point, rather than using a parented camera. However, it's probably not a bad place to start when prototyping a game.

You could explicitly set the y value of the camera in LateUpdate(). This would mean that every frame, after the movements have all taken place, the camera y position would be set back to the value that you want. You can use camera.main to find the main camera, and then you don't even need to put this script on the camera.

function LateUpdate()
   camera.main.transform.position.y = 2.0;  

hey i have the same problem as u.and i used this code late update and it worked well! :wink:
actually im making a 2d game just like super mario.and i have a lil problems with coliding with for example walls and passing through them!!
and also a lil problem on my player jumping . i really appreciate if u let me see your code for your player script . maybe it would help me.tnx alot .

If you really just need a super simple camera that follows only along X (once you move past parenting), this will do it for you, and is easy to grow from.

var player : GameObject;

var depthOffset = -10;
var verticalOffset = 0;

function Update () {
	transform.position.z = depthOffset;
	transform.position.y = verticalOffset;
	transform.position.x = player.transform.position.x;

Drag the player character onto the Player variable in the Inspector, and it’ll just work. Adjust your offset parameters as needed.