- Home /
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: http://www.youtube.com/watch?v=lpk5U70MQBg
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!
-Zach
Thanks $$anonymous$$rWhoof I was in the same predicament as the other guys and your script worked wonders I lost most of my hair trying to figure it out and you had it in 10 lines of code, much appreciated.
Answer by · Sep 26, 2010 at 11:19 PM
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;
}
Thanks $$anonymous$$arowi, it worked perfectly! I'm a scripting newb, so the help is much appreciated. I'll definitely look more into controlling the camera via script (I tried a bit, like having the camera move left and right when you press the left and right buttons to move the character, but I kept running into issues). Thanks again!
No troubles at all. Please remember to vote up answers that help, and mark them as correct (click the tick beneath the arrows) if it solves your problem. Cheers!
Answer by shahinexir · Aug 10, 2012 at 08:31 PM
hey i have the same problem as u.and i used this code late update and it worked well! ;) 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 .
Answer by MrWhoof · Oct 06, 2012 at 08:42 PM
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.
Thanks $$anonymous$$rWhoof I had the same predicament as the other guys and your code saved me from losing any more hair.
@$$anonymous$$rWhoof Thanks a lot man.. I would really appreciate if you could explain this code :)