- Home /
How to make a 2d game with fixed camera rooms
Hello.
I want to make a 2d platformer but this question spans more than just platformers. In my game, I want the player to discover the world screen-by-screen, instead of having a camera smoothly follow the player around. The kind of behaviour I mean can be seen in games like Knytt Stories or VVVVVV
What would be the proper way to implement this kind of behaviour in Unity?
I can think of two options but I don't know which one is better, or if there is yet another, even better way.
Make a separate scene for each screen. This means no slowdowns on mobile devices, but it might be a hassle to construct, as you have to re-create the player object and the camera each time. It also makes it hard to keep track of how rooms fit together.
Make a single scene and have the camera follow in fixed steps. This might get slower on mobile devices. It is easier to keep track of where everything is, however. But how to ensure that the room sizes are the same each time?
I hope someone can shed light on this dilemma.
Answer by Le-Capitaine · Apr 09, 2014 at 11:51 AM
If you're going for the multiple-scene thing, you can use DontDestroyOnLoad(this)
on your objects' scripts. Still, one single scene is probably the way to go.
For the latter, what you could do is create a prefab with a box trigger the size of the screen and a script with a public Transform
; within that script, you'd set the camera's transform.position
to the specified transform.position
when the player triggers it.
public Transform camTarget;
void OnTriggerEnter(Collider other) {
if (other.tag == "Player") Camera.main.transform.position = camTarget.position;
}
Then you'd simply put that object in the center of each room and set the coordinates.
If you're going for 2D, you can do it without even a variable, like so:
void OnTriggerEnter2D(Collider2D other) {
if (other.tag == "Player") Camera.main.transform.position = transform.position + new Vector3(0,0,-10);
}
and then the camera is automatically placed at the center of each room, with the camera 10 units away from the action.
Answer by BenBonkGames · Dec 08, 2019 at 02:17 AM
A more simpler way to do this would probably be Cinemachine if anyone found this thread from google. I made a tutorial on how to do something similar so let me know if it helps you out. https://www.youtube.com/watch?v=yaQlRvHgIvE