- Home /
2D, lock the "y" axis
Hello, I was wondering how can I look the camera in the "y" axis in order for my camera to only follow my character on the "horizontal axis". How can I do this? Just like super mario, where the camera follows Mario while moving forwards and backwards but never when he jumps.
Thanks! :D
Answer by slavo · Nov 25, 2016 at 10:15 AM
Hello,
you need to write custom follow script for your camera. Basic implementation can look like this.
public class FollowScript : MonoBehaviour
{
public Transform FollowObject;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
Vector3 pos = new Vector3(FollowObject.position.x, transform.position.y, transform.position.z);
transform.position = pos;
}
}
You need to attach script to camera, drag and drop your character game object to FollowObject property and make sure camera is not child of character in Hierarchy window.
You will need to add more code to update function, like offsetting, smoothing etc. to have it look really good, but this is basic idea behind camera control.
Your answer
Follow this Question
Related Questions
How do i lock out a axis 1 Answer
How do I lock Y Axis movement for Parented Camera? 3 Answers
Camera moving in one direction? 2 Answers
How can I make a flying object follow ground player and stay airborne? 2 Answers
Ray Casting in 2D 1 Answer