0 Favourites

Smooth camera movement

  • 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.

    <img src="http://puu.sh/2p5tr.jpg" border="0" />

    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.

  • Construct 3

    Buy Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Buy Now Construct 3 users don't see these ads
  • 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.

  • 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).

  • 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.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)