- Home /
How to stop view from scrolling when edge of view collides with a game object?
Hello, I am coding a game with two players and I am having an issue figuring out how to stop the camera from scrolling when a player walks too far from the other player. The way I have it set up is that once a player touches a certain point near the edge of the view, it clamps the players position (both left and right side of view) so that the object doesn't go off screen.
However, if I keep moving my player one, the view continues to follow player one and drags player 2 along for the ride against the other edge of the view. Instead of this happening, I want the view to not allow player 1 to continue if the other is against the view border.
So far, I have this is the code I am using for what I have thus far.
Vector3 viewPos; //Variable to convert world space to view port space
Vector3 delta; //Change in camera position relative to player1 position
Vector3 destination; //Variable to hold camera's change in position to keep players in view
//Variables to hold a value to measure the borders on screen to contain players
float leftBorder = cam.ViewportToWorldPoint(new Vector3(0.06f,0,dist)).x;
float rightBorder = cam.ViewportToWorldPoint(new Vector3(0.95f,0,dist)).x;
//Clamps the players position onto the camera view
player1.position = new Vector3(Mathf.Clamp(player1.position.x,leftBorder,rightBorder),player1.position.y,0);
player2.position = new Vector3(Mathf.Clamp(player2.position.x,leftBorder,rightBorder),player2.position.y,0);
if(player1)
{
viewPos = cam.WorldToViewportPoint(player1.position);
delta = player1.position - cam.ViewportToWorldPoint (new Vector3(0.5f,0.5f,viewPos.z));
destination = transform.position + delta;
destination.y = 0;
transform.position = Vector3.SmoothDamp(transform.position,destination, ref velocity, dampTime);
}
Before I decided to ask for help, I was trying to find a solution right before the last line of code by creating an if statement to check if the players.x position (player1.position.x) is greater than the view then perhaps stop the view from scrolling, but it wasn't working for me and I'm not sure how to halt the position of the camera.
Your answer
Follow this Question
Related Questions
How can I get the bounds in PolygonCollider2D? 0 Answers
Keep player from falling outside the camera? [2d] 1 Answer
CustomCameraController: Rotating my camera breaks my out of bounds code 0 Answers
How to Calculate the border of Camera in Specific Depth? 0 Answers
2D camera rotating relatively to game object speed 0 Answers