Wrong position with lerp()

0 favourites
  • 4 posts
From the Asset Store
Be quick and choose whether the shown equation is right or wrong.
  • Hi,

    I have a little problem with the lerp() function. I want move multiple platforms to a random position. I user lerp() to "animate" the movement. It's already fine with the first instance, which slide up from the bottom of my layout. But the second instance, which slide in from the top of my layout stops at the wrong position.

    I have two instance variables. One of them is the "CurrentPosition" and the other one is the "DestinationPosition". The lerp() function will run, if one of them is not equal to the other. The second instance will stop moving ~20-30 pixel before the "DestinationPosition" reached.

    The objects (e.g. platforms) spawn outside of my layout.

    What's the problem here?

    Here is a link to my file: ~link removed

    Thanks for any help.

    ~edit:

    So here is a example: The layout height is 1000 pixel. Platform A move from 1100 to 800 pixel. Platform B move from -100 to 200 pixel.

    So, I think there is a problem with negative start value of platform B?! How can I solve this problem?

  • Honestly I don't know why it doesn't work, it should. It stops 39 pixel before the destination. You use int so all values smaller 1 will not be added, but 39 Pixel should be around 2 pixel. Without int it stops around 20 pixel before. If you change the action in event 8 to:

    "Platform|Set Y to self.y + (lerp(0, abs(Platform.CurrentPosition - Platform.DestinationPosition, 0.05)"

    it works. But that should be exactly what you have in your event (without int).

    Maybe someone with more knowledge can help.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I think it's a combination of two things. First of all, you definitely shouldn't use int() here, as this will stop the object about 20px early.

    Another issue is Physics. It's not recommended to move Physics objects with non-physics actions. What happens is - Physics behavior is "resisting" and adjusts platform position, moving it a fraction of a pixel back. For example, if lerp set platform at Y=500, Physics moves it to Y=499.99995. Then you use int(self.y) and CurrentPosition value becomes 499. As a result, your platform can not move further.

    If you remove int() and disable Physics, then your code will work correctly.

    .

    I suggest using LiteTween. You can achieve the same effect and it will stop at the exact position (unlike lerp, which gets infinitely close, but never actually stops). And then when the tween is finished, you can enable Physics.

  • If you remove int() and disable Physics, then your code will work correctly.

    Yep! Thank you, that's it.

    The solution:

    - Disable physics behavior

    - Move platform with lerp to destination position

    - Activate the physics behavior

    ~closed

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