Move camera to show exit, and then back to player

I have the camera following the player in a physics based platformer. It’s working great with LERP.

But, testers have said that sometimes they can’t find the exit. So, I want to have a few locations in each level where a player can collide with a sprite that will cause the camera to move away from the player and toward the exit, then back to the player and continue as normal with my previous settings for following the player.

This is way over my head. Any ideas on how to do this?
Thanks!

A group of events like these can do the job.

You can adjust the pause, and even use LERP to avoid a sudden camera change-

I hope it helps

1 Like

Thank you so much! I had just copied and pasted the code for the LERP used in my game, so I don’t really know how to implement it here.

This is what I have, based on what you showed as an example.

How do I impliment LERP to and from the exit assuming that will show the whole camera move?

Hi, instead of centering the camera to exit, you could use Change the X position of camera + Change the Y position of camera to the exit x and y coordinates with lerp. You can check how it works here: GDevelop 5

2 Likes

I’d look at using a positional tween. Tween from the current position to the exit, pause for a second, and then tween back. It’ll be smoother and take up less coding.

Remember to stop actioning player touch input while the tween is playing.

3 Likes

Thanks for the suggestions @Drona and @MrMen

I didn’t know we could tween the camera position. That opens up all sorts of interesting possibilities!

To pause the tween, would I use a timer? Or is that something I can build into the tween. I’ve only used a tween function 2 times so far so I’m still getting used to it.

Can I tween to an object (the exit sprite), or will I have to type in the x/y coordinates? I hope I can tween to the exit sprite since it’s in a different place on each level, and would mean having to know that location and have different code for each level.

One way is to have an invisible/hidden object with tween behaviour that the camera is centred on. Move it from the player to exit.


You could use a timer. Or you could use a tween to pause - say use a positional tween that moves to the it’s current poistion over however any milliseconds you want to pause.


You’ll have to input co-ordinates, though you can use the X & Y values of an object, which in this case may be something like exit.X() and exit.Y(). A position tween will move the object from it’s current position to the position you specify as the tween parameters.

2 Likes

I am already using a “follow” object for the camera as seen here:

Should I tween that object to move to the exit sprite to have the camera animate from the main character’s position to the exit, and back?

I tried to figure out how to keep going on this without asking for more help, but I’m totally lost. As it is, I don’t understand how the follow object is having the camera follow the main character object. But it is doing that.

Yes, though you’ll have to modify the events in that screen snip to check whether the game is playing , or the exit is being shown, and working out if the update is to be done using those lerp actions.


It’s the lerp action that does it. The lerp command takes 3 parameters - valueA, valueB and a ratio (a number between 0 & 1) - and return a result, valueC. If you imagine there’s a line with valueA at one end, and valueB at the other, then valueC the ratio away from valueA along that imaginary line

So, say you have valueA is 20, valueB is 30 and ratio is 0.25 (=1/4). Then lerp would return the value thats 1/4 of the way from 20 to 30, which would be 22.25. If the ratio is 0.8, then the lerp value would be 28.

Does that start to explain it?

I appreciate the explanation, but it doesn’t really make sense to me. I’ve never really understood maths and the related studies like geometry, trigonometry, etc.

If the follow object doesn’t move, how does the camera know to follow the moving sprite of the main character? And how will the follow object know where the main character is when triggering the tween? I don’[t think there is any code or actions that connect the “follow” object to the main character sprite.

Should I use a scene variable for that?

Does this help :

image

If you use lerp(A, B, 1/3), it will return the value C, which is 1/3 of the way between values A & B.

Or “If I’m at A and I want to get 1/3 of the way to B, where will I be?” (answer = C)


  1. The first positional tween on the Follow object you’d use would be to Exit.X(), Exit.Y(), over 2000ms and name it “ToExit”. This will move the Follow object to the exit object over 2 seconds. Also set the object variable to indicate Follow is in the show-the-exit stage.

  2. Once the “ToExit” tween has finished, delete it and create another postional tween, again to Exit.X(), Exit.Y() over 1000ms and name it “WaitAtExit”. This won’t move the Follow object anywhere, doing nothing for 1 second (effectively pausing the show-exit stage).

  3. Once the “WaitAtExit” tween has finished, delete it and create a final positional tween to MainChar.X(), MainChar.Y(), over 2000ms and name it “BackToPlayer”.

  4. Once the “BackToPlayer” tween has finished, delete it and set the object variable to indicate it’s back to playing.

It does move - the first action of the subevent sets the new position.


You can, or you can add an object variable to the Follow object to indicate whether it’s in play or show mode.

I see that, but it doesn’t say anything about the main character sprite. So I don’t understand how the follow object knows where the main character is. I suppose it’s not necessary to understand, but it would probably help me to understand other things in the program if I did.

I think I understand what LERP is doing based on your description. Thank you.

Here, in the lerp function, it references MainChar.X() and MainChar.Y(). That’s the value B for my example in my previous post :

That position is always changing, so the lerp result is always changing.

This is one reason I’m having so much trouble with programing. I looked at those actions at least a few dozen times trying to see “MainChar”, and didn’t see it until you just pointed it out. :frowning:

I don’t know how my brain does that.

Thanks again. I think I understand your explanation on what to do with tweening. I’ll spend some time working on the implementation and let you know how it turned out.

1 Like