Camera Movement Question - How to follow player in Y axis only above certain threshold?
Hello, this is my first post to the forum. I am creating a 2D platformer game and cannot seem to get the code quite right to achieve the effect I'm looking for. I want the camera to follow the player in the X axis only (which I have it doing now) and then when the player jumps above a certain threshold, they kind of drag the camera up with it so the player is centered slightly higher than when at ground level. I also want to reverse the effect when returning to ground level. I have tried using a boolean to check whether the player is above a certain height, then apply a transform to the camera, but it come out looking choppy, and then the camera never goes back to its original y position. Here is my code for the camera:
void Update ()
{
if (player.GetComponent<PlayerCtrl>().topOfScreen == false) //freezes camera in y direction if player is not at the top of screen
{
//makes the camera follow the player in the x axis only
transform.position = new Vector3(player.position.x,transform.position.y, transform.position.z);
}
if (player.GetComponent<PlayerCtrl>().topOfScreen == true)
{
//makes camera follow player in the x and y axis
transform.position = new Vector3(player.position.x, player.position.y - 2, transform.position.z);
And the code fore the boolean checking when the player exceeds the height when I want the camera to start following in the y direction: [code=CSharp] void ScreenCheck() { topOfScreen = false;
if (transform.position.y > 4)
topOfScreen = true;
if (transform.position.y < 4)
topOfScreen = false;
Mostly I want to get a solid understanding of why it isn't working now and how I can make it work so the transition is smooth. Thanks for any help!
Joe
Your answer
Follow this Question
Related Questions
How to detect if ObjectA is looking at ObjectB, and vice versa? 2 Answers
Trouble with directing launched projectiles in Unity 2D 0 Answers
How can make it so only 1 object of type “Ability” can be selected at once? 0 Answers
Scrolling background wall collision problem 0 Answers
Shooting bullets up (y axis) 0 Answers