Make a Sprite Move A Fixed Amount Left/Right

0 favourites
  • 10 posts
From the Asset Store
A complete set of graphical user interface (GUI) to build 2D video games. With wood, nature, & jungle theme
  • Hello!

    I am making a small infinite runner game with three 'paths' like Subway Surfers. You will be able to tap left/right to move to the path to either your left or your right. How would I go about making it so that the sprite moves smoothly a fixed amount left or right? (160px, to be precise)

    Thank you,

    OrangeFlash81

  • I assume this is top-down and you're moving along the X axis.

    Make a variable "position" and a variable "speed", on start of layout set the "position" variable to the players X position, set the speed to 1 in the beginning. On left or right click, add or subtract 160 to/from the position variable. Then every tick set player position to:

    lerp(Self.X, position, dt*speed)

    If you need help understanding lerp (linear interpolation) I recommend the links in the FAQ in this subforum.

  • OK, thanks for the very quick reply! I'll give that a go; I've never tried anything like this before. Can I just as what 'dt' actually does!? I've seen it a lot over the forums...

  • Sure thing, it's a bit hard to explain so I'm just gonna redirect you to this handy tutorial.

    If you have any problems, feel free to ask and I'll put together an example for you or something.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • The script works fantastically! I have another question, and I'll see if you know the answer, Nimtrix ; How do I make the ScrollTo behaviour focus higher on the object? What I mean is, making my sprite appear at the bottom of the screen rather than bang in the middle. Is this possible?

    Thanks?

  • You could use the system action

    Scroll to X,Y as ;

    X = Player.X-(Fixedamount)

    Y = Player.Y

  • Yeah, skip the behaviour and control your camera with events. Use every tick as the condition, then "System -> Scroll To position" actions allows you to set the exact point for the scrolling, and if you use the lerp expression for this as well, you'll get smooth scrolling. (if needed of course)

    Then you just set it to scroll to Player.Y - Offsetamount.

    Or for smooth:

    lerp(ScrollX, Player.X, dt*scrollSpeed)

    lerp(ScrollY, Player.Y - offset, dt*scrollSpeed)

  • Thank you! That method works great, Whiteclaws.

  • Nimtrix , Ninja'd ;)

  • Whiteclaws

    Indeed, at least I provided some additional info, so not completely useless. <img src="smileys/smiley14.gif" border="0" align="middle" />

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