- Home /
Lock main camera on x-axis
Hi, I'm making a 2D Platformer, how can I lock my camera on the x-axis so it doesn't follow when I jump? Also I need to make it so my camera's rotation is locked, any idea on how to do that?
Answer by rutter · May 16, 2012 at 05:41 AM
Your camera won't move unless you have somehow asked it to. Perhaps you've attached a script component, or it is parented to something which moves? These are significant choices. That's not to say they're bad, but you should be aware that you've made them.
I've found this is nearly inevitable for any project of appreciable size: you will probably need to write at least one script to manage your camera's position and rotation. For example, the script could assign the camera's position on every frame by matching the player's transform position on one axis only. The nature and scope of this script will depend quite a lot on what exactly you'd like the camera to do.
Manipulating transform.position, transform.localPosition, and so on will usually get you more or less what you want. transform.LookAt() can also be pretty useful. You can see all of this and more at the Camera script reference.
I realize this isn't a very specific answer. To solve your immediate problem, you could assign a particular value to the camera's position on the one axis every frame.
If you do write a camera control script, it's usually going to be one of the last things you'll do on any given frame; you can set a script execution order, or use LateUpdate().
Answer by ilovetree · Aug 10, 2016 at 12:59 AM
I think this is funny; I gave my camera a rigidbody, parented it to the controlled game object, and locked the axes.
Your answer
Follow this Question
Related Questions
Problem With 2D Camera Following Target 3 Answers
Main Camera child under player, causes flip 3 Answers
Free Look Camera Pivot 1 Answer
On Mouse Drag Game Object follows Mouse on a single Axis. 1 Answer