Help with a some code I found online

0 favourites
  • 5 posts
From the Asset Store
Game with complete Source-Code (Construct 3 / .c3p) + HTML5 Exported.
  • I found a tutorial on that I am trying to understand, but the tutorial has no commentary. Could someone please explain how to the code is doing what it is doing. I can replicate the tutorial with no problem, but I don't understand it enough to change things in it for the desired effect. Thanks for your help.

    The code is in the video at 9:20

    lerp(scrollx,(clamp(Player.X, Zone.X+(160),Zone.X+Zone.Width-(160))),0.1*60*dt)[/code:1nsem39b]
    I kind of understand lerp and the three descriptors that it needs, I'm not quit sure about clamp, I understand sprite coordinates, I understand that the 160 is the midpoint of the "Zone" sprite. The last part 0.1*60*dt is what is getting me, I think. I also have no idea what scrollx does exactly...
    I am trying to get it to work so that the screen is 1920*1080 and the rooms are the same size and it transitions smoothly to the next room. The rooms will be on a grid thats 10x10. 100 variables for the rooms is just too much, and this seems a lot cleaner.
    [edited for clarity]
  • Hi ,

    Looking at the video, this code is firing every tick whilst the player is overlapping a zone sprite (in fact you don't need the "every tick" condition, just checking for the overlap should trigger every tick). Whenever the player sprite moves over a new zone sprite it repositions the camera using lerp to ensure a smooth transition.

    Breaking the code down:

    lerp(a, b, x) is the linear interpolation of a to b by x. X is usually a value between 0 and 1 representing the percentage of the distance between a and b to travel.

    clamp(i, lower, upper) returns lower if i is less than lower, upper if i is greater than upper, else return i. This is a handy expression to restrict the range of a variable.

    So in this example, every tick the camera scroll position will move 0.1*60*dt (about 10) percent of the distance from the current camera scrollX position(a) towards the Player.X position(i), limited by the clamp expression to between 160 pixels from the left(lower) and right(upper) of the current zone, so if the player moves outside of that area the camera won't scroll any further until the player moves into the next zone.

    If the player changes direction and reenters the central area of the zone then the camera will start to follow them again.

    Hope that makes sense!

  • If you want to modify this code, increase both instances of the value 160 to reduce the area that the camera will scroll within a zone, and reduce the 0.1 in "0.1*60*dt" to make the camera move slower.

  • Okay, I am trying to get this to work and I just can't figure it out. I have attached the capx if anyone would like to take a look and explain what I am doing wrong. Thank for all the help! ... DFxWWZSbkU


    Okay, so i got it working perfectly, here is the code that worked;

    X Code

    lerp(scrollx,(clamp(spr_Player.X, spr_Room.X+(0),spr_Room.X+spr_Room.Width-(1920))),0.1*60*dt)[/code:1g8g8hbl]
    Y Code
    [code:1g8g8hbl]lerp(scrolly,(clamp(spr_Player.Y, spr_Room.Y+(0),spr_Room.Y+spr_Room.Height-(1080))),0.1*60*dt)[/code:1g8g8hbl]
    This also allows for larger rooms as long as they are multiples of 1920x1080
    Thanks for all of the help!!!
    [b][EDIT 2][/b]
    So larger rooms aren't working right...I will post here if I can figure it out...
  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Hi bizzy401,

    The link you posted for the capx is contracted - you need to post the full link for us to be able to download it.

    Some things to check before you update the link:

    1) Is your origin point for spr_Room in the top left corner?

    2) This statement...


    ...means that if a room is 1920px width then your bounding box for the camera is going to run from the left edge to... the left edge. Similar story for the Y axis. I'm pretty sure that's not what you want. I'm guessing you'd have more luck with something like:

    spr_Room.X+960, spr_Room.X+spr_Room.Width-960

    That would centre the camera in a room that was 1920 pixels wide and for wider rooms stop the camera moving 960 pixels from the left and right edges. For the Y axis use 540.

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