Smooth camera movement

Get help using Construct 2

Post » Wed Mar 27, 2013 8:44 pm

Hi.
So I'm basically trying to get my camera to smoothly move towards the center of the position between the character and the mouse pointer. This is the code I wrote for it.



This works fine but it's not smooth at all. I was experimenting with movement a bit, but it's not working well. Anyone have tips?

Additionally, how can I alter the equation I wrote to make the camera appear closer to the player instead of at the halfway point? Like perhaps 1/3rd of the way between the player and the camera.Sushin2013-03-27 20:48:05
B
10
S
3
G
3
Posts: 234
Reputation: 3,467

Post » Thu Mar 28, 2013 1:53 am

One hackish solution would be to create an empty sprite and have it move towards the position you want to scroll to. Maybe give it a bullet behaviour. Set the camera to always scroll to this sprite. You can change scroll speed by altering the bullet speed.
B
14
S
6
G
1
Posts: 8
Reputation: 1,559

Post » Thu Mar 28, 2013 2:16 am

This is a very simple and effective way to do this:

-Create your player sprite, which I'll call Sprite.
-Create an empty sprite (something small like 4x4), no fill... I'll call it Camera
-Add the "Scroll To" behavior to Camera (the sprite).
-One simple event:

Every tick | Camera - Set position to
X: lerp(Camera.X, Sprite.X, 7*dt)
Y: lerp(Camera.Y, Sprite.Y, 7*dt)

You can change the "7" to whatever you want. Bigger numbers are faster, smaller are slower. 1*dt mean it would take one second for the Camera to reach the Sprite. The game window will scroll to the Camera rigidly, but the Camera will scroll smoothly to the player (making the camera move smoothly).
B
90
S
32
G
14
Posts: 935
Reputation: 14,540

Post » Thu Mar 28, 2013 2:48 am

Wow thanks. I really need to understand how lerp works.
I had to tweak it a little bit but I got it to work.

For future reference, my code now is this:

lerp(Camera.X, ((Player.X + Player.X + Mouse.X)/3), 12*dt)

This will make the camera focus on a point between the mouse and the player in a smooth fashion, kind of like looking around with the mouse.

This part "((Player.X + Player.X + Mouse.X)/3)" is basically averaging the distance between the player and the mouse, except its adding the player twice, so the average position ends up being closer to the player.Sushin2013-03-28 02:54:20
B
10
S
3
G
3
Posts: 234
Reputation: 3,467


Return to How do I....?

Who is online

Users browsing this forum: Artpunk, Axiomaltd, MACPK, nicklowkcmagic, OddConfection, radbrothers and 24 guests