How to stop the camera when the player has reached the edge of the level?
I have viewed some tutorials and they do work but it's a bit finicky and the camera does not re-center on the player when respawned.
I'm making a 2D platformer game where the camera is centered on the player and follows the player around. However, I don't want to make the camera see past the edges of the level but I still want the player to be able to move around and touch the edge. Also, the camera should follow the player around again once the camera is centered on the player again. Can someone please explain how I can go about this? Thanks.
Answer by Dibartolomeo · Jul 31, 2016 at 03:22 PM
Im not sure, but try to use a simple operation with Mathf.Clamp combined with the Update().
Answer by SebasSBM · Jul 31, 2016 at 06:14 PM
I would try to fix such kind of issue by limiting the camera move using a Rect, or rather better, a Collider2D set as "isTrigger" that covers the whole level and defines their bounds (or better said, bounds that cam shouldn't cross).
So, with the "trigger collider" defined, you can attach this to the cam's script to control events when the cam goes out of the "trigger collider" (ALERT: The snippet is in Javascript. You might need to convert it's syntax to C# if you are using C#):
function OnTriggerExit2D(other : Collider2D) {
// I assume your cam has the tag "MainCamera" attached
if (other.tag == "MainCamera") {
/* Whatever logic you might need to implement in order to limit the camera's moves.
* You might need to do coord checks to see if cam went out of bounds by right,
* left, up and down, and limit it in a different way depending on this
*/
}
}
I strongly reccommend to read carefully my snippet rather than copy-pasting blindly. Is much more like a template rather than a complete snippet of code. I would complete it, but due to I diidn't see your code, I can only guess how you made it.
It might also be helpful for you to see the features of OnTriggerExit2D method, as well as OnTriggerEnter2D and OnTriggerStay2D (all them methods of the MonoBehaviour class), as well as the features of Collider2D class.
And, in general, you can find complete documentation about any class or method used by unity in docs.unity3d.com
Ok, thanks for your help! I deleted the code attached to the camera but I can give you the code which I found in a tutorial which I based my own script off, modifying it slightly to work with the game of course. It worked fine but the only problem was the issue of re-centering the camera on the player.
From here: http://answers.unity3d.com/questions/600429/stop-movement-at-edge-of-screen-2d-shooter.html
// X axis
if (transform.position.x <= -4.3f) {
transform.position = new Vector2(-4.3f, transform.position.y);
} else if (transform.position.x >= 4.3f) {
transform.position = new Vector2(4.3f, transform.position.y);
}
// Y axis
if (transform.position.y <= -2.7f) {
transform.position = new Vector2(transform.position.x, -2.7f);
} else if (transform.position.y >= 2.7f) {
transform.position = new Vector2(transform.position.x, 2.7f);
}
It would work. The inconvenient of this approach is that it uses hardcoded values, which is a bad practice.
Below I post the same code, but without hardcoded values (Javascript). This way the code is more "human-readable" and it becomes easier to change the values.
// Values with a meaningful variable name
var leftBound : float = -4.3f;
var rightBound : float = 4.3f;
var upBound : float = 2.7f;
var downBound : float = -2.7f;
// X axis
if (transform.position.x <= leftBound) {
transform.position = new Vector2(leftBound, transform.position.y);
} else if (transform.position.x >= rightBound) {
transform.position = new Vector2(rightBound, transform.position.y);
}
// Y axis
if (transform.position.y <= downBound) {
transform.position = new Vector2(transform.position.x, downBound);
} else if (transform.position.y >= upBound) {
transform.position = new Vector2(transform.position.x, upBound);
}
It might be good for you to learn more about variables and functions in Unity.
PD: About the "re-centering the camera" issue, it would be hard to resolve using this approach. It would probably be easier re-centering the camera using the "trigger collider approach"
@Threadlight There's also another problem with your question: I don't know how you implemented camera following. "Did you made the cam a child of the player Gameobject? Or do you move the camera programmatically?" Even though I am willing to help you, without knowing this information it is hard to give you a good answer.
I invite you to read this website's user guide and FAQ section to obtain tips about how to make good questions. This way, users willing to help like me will have it easier to help you. You can edit this question to make it better, just click on the $$anonymous$$achine Cog logo next to the question title, and click Edit.
One more thing: adding Javascript
or C#
tags to your question will let other users know which program$$anonymous$$g language you are using, avoiding unnecessary misunderstandings.
Your answer
Follow this Question
Related Questions
Camera not following 2D character when the character reaches the ground. 0 Answers
Why is my camera shaking when I use my chaser script on something? 0 Answers
Does anyone know the script for a smooth camera follow of the main game object? 8 Answers
When attaching my custom camera script camera shakes when player starts to move fast. 0 Answers