Slide sprite at angle relative to current position

0 favourites
  • 6 posts
From the Asset Store
3 different games for kids, easy to edit, mobile ready
  • In my top down shooter when my player sprite collides with my enemy sprite, I want the enemy sprite to smoothly move/slide 500 pixels away from my player sprite (relative to the enemy's current position). The speed of movement would depend on the enemy type.

    Looking at similar posts I think I need to use lerp but not sure how this would incorporate both x and y movement and the angle of movement. Would it require separate actions?

    Any suggestions appreciated.

  • I think this would work (Image here):

    Add these instance variables to the enemy:

    "timer" starts at 100

    "playerX" and "playerY" starts at 0

    Events:

    Enemy collision with player

    Enemy set "timer" to 0

    Enemy set "playerX" to 0

    Enemy set "playerY" to 0

    Enemy "timer" < 100

    Enemy set "timer" to clamp(timer+(<font color="blue">10</font>*60*dt),0,100)

    Enemy set X to

    self.x + (cos(angle(self.x, self.y, self.playerX, self.playerY) ) *-1* lerp(0, <font color="red">30</font>, self.timer*0.01))

    Enemy set Y to

    self.y + (sin(angle(self.x, self.y, self.playerX, self.playerY) ) *-1* lerp(0, <font color="red">30</font>, self.timer*0.01))

    <font color="blue">10</font> is the speed at which it moves.

    <font color="red">30</font> is the distance

  • Have tried this out but the movement doesn't seem to be right. I'm aiming for "soccer ball" type movement when the enemy is kicked by the player.

    Have created an example: copy.com/BlfuSTiwuoqy

  • Here's an alternative with Bullet and Timer.

    EnemyHitTest_BHT.capx

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • That seems to work fine, thanks.

    Is there any way to get the enemy to slow to a stop rather than immediately stop? Have tried setting gravity to a negative value before the timer action but this doesn't seem to work.

  • Set the Acceleration to a negative value. You'll need to play with the numbers.

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