Home > Back-end >  Attach camera to player in the y direction while x and z are static
Attach camera to player in the y direction while x and z are static

Time:04-19

I'm programing a 2D platformer, and I'm trying to make it so that the camera follows the player unless the player is near the edge of the level, in which case the camera will stay in one position until the player moves away from the edge of the level again. Pretty standard stuff.

I've accomplished this by placing a trigger area near the edge of the level, and once the player enters the trigger area, the camera will stay in one spot. It works well, except that the camera stays still no matter what the player does. I want it to stay still in the x-direction if the player is trying to run to the side, but I would like it to follow the player in the y-direction if they jump.

This uses two scripts. Here is the code on the player:

public class Eleni_Controller : MonoBehaviour {

public GameObject _mainCamera; // assigned in inspector
public Vector3 cameraEdgeRight = new Vector3(0,0,0); // position assigned in inspector
public Vector3 cameraEdgeLeft = new Vector3(0, 0, 0); // position assigned in inspector
public bool atEdge = false;

void OnTriggerEnter2D(Collider2D col)
{
    if (col.gameObject.name == "camera_edge_trigger_right")
    {
        _mainCamera.transform.position = cameraEdgeRight;
        atEdge = true;
    }
    if (col.gameObject.name == "camera_edge_trigger_left")
    {
        _mainCamera.transform.position = cameraEdgeLeft;
        atEdge = true;
    }
}
void OnTriggerExit2D(Collider2D col)
{
    if (col.gameObject.name == "camera_edge_trigger_right" || col.gameObject.name == "camera_edge_trigger_left")
    {
        atEdge = false;
    } 
}

And here is the code on the main camera:

public class Camera_Behaviour : MonoBehaviour {

public Vector3 cameraPositionDefault = new Vector3(0, 4, -10);
public GameObject eleni; // player object, assigned in inspector
private Eleni_Controller eleniController; // script controlling player
private Transform playerPosition;

void Start()
{
    eleniController = eleni.GetComponent<Eleni_Controller>();
    playerPosition = eleni.transform; 
}

void LateUpdate()
{
    if (eleniController.atEdge == false) // if player in not close to either egde
    {
        transform.position = playerPosition.TransformPoint(cameraPositionDefault); // sets camera's position to player's position
    }  
}

I can't figure out how to program it so that all of this only affects x, but will still follow the player's y position.

Also, if there is a more efficient way to program this, I would love to hear it! Thanks!

CodePudding user response:

Maybe take a look at Cinemachine built-in component and set up two cameras with blending control

CodePudding user response:

I ended up fixing it by adding a new vector3 in late update, which defines the x, y and z values separately, and adding an "else" statement to use when the player is at the edge.

void LateUpdate()
{
    Vector3 cameraPositionEdge = new Vector3(transform.position.x, playerPosition.position.y   1.6f, transform.position.z);

    if (eleniController.atEdge == false) // if player in not close to either egde
    {
        transform.position = playerPosition.TransformPoint(cameraPositionDefault); // sets camera's position to player's position
    }
    else
    {
        transform.position = cameraPositionEdge;
    }
}
  • Related