How do I add a knockback properly in my platform game?

0 favourites
  • 15 posts
From the Asset Store
Customize the animation of character when item changed
  • Hi guys,

    So I'm learning Construct 3 being new in game development & I'm trying to learn it by making a simple platform game wherein the player has to go through levels and beat enemies to win the game.

    I'm currently facing a problem with player knockback when it's hit by an enemy sword.

    I'm pasting the screenshot of the event sheet where the logic of knockback is written below -

    I'm using sprite boxes for player, enemy, player sword & enemy sword so basically the idea is if the enemy attacks the player, the player will take a hit, flash & have a knockback if the player's X is less than the monster's x then the player's X vector is set to -400 from current X & if player's X is greater then monster's X then player's X vector is set to +400 from current X.

    However this code is not working as I think it should, instead please see the video below of what's happening -

    Subscribe to Construct videos now

    So basically, when the player is standing on the left side of the screen from the monster (Player's X is less than Monster's X) the player is getting knocked back into the monster rather than getting pushed behind, however, it's correct when it's the other way around. Also for some reason, the knockback animation is not getting played every time only sometimes.

    I've tried a lot of conditions but I know I'm doing something wrong here just not able to figure it out maybe that's not how you do a knockback? Can someone please help & point me in the right direction?

    Thank You!

  • Is there more than one enemy in the level? Those events don't pick an enemy.

  • Not at the moment but yes I plan to add more enemies to the level

  • I mean literally in the layout are there any more instances of enemy on the outside that you can't see? Is there only one instance of enemy?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Only one at the moment

  • In the sub event with system conditions try replacing them with the enemy object condition pick compare X against the player collision box and see what happens.

    The problem is even if that fixes it, you will come across a bug when you add more than one enemy. If the green object is in a 'container' with an enemy then it could relate well. The problem you have is green object colliding with player doesn't relate to any enemy.

  • Since it's already picked, can you use the green objects X instead to compare?

  • Was doing that only by comparing vector X but the same thing was happening that's why tried changing to system condition instead-but same result :(

    How can I create this container which relates to the enemy? Sorry I didn't understood :(

  • You can use the green object X instead of the enemy x, will that work in your game? Not comparing vector X, compare X the position it's a different condition.

  • There's a box that is set in position with an image point on the enemy's sword at every tick and I'm checking if this box collides with player's collision box when enemy is attacking

  • Should I share the c3p file to understand better on what's happening?

  • Yeah I can see that, the problem is that the box doesn't relate to the enemy. You can use the box instead of the enemy though (probably). Or a container. Share the c3p I will edit it.

  • The main issue I see now it is because on the vectorX actions you've used self.x needs to be self.platform.vectorx.

    Probably you will have an issue if you add more than one enemy, this is the picking I mentioned earlier. I would just use the collision box X and not the enemy as I've edited it here.

    dropbox.com/s/nhuxbhvvvnudbmm/castle_raider%20%281%29.c3p

  • Thank you so much, this worked 😊

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