Playmaker Forum

PlayMaker Help & Tips => PlayMaker Help => Topic started by: maxrostov on November 10, 2020, 03:40:01 PM

Title: 2D player knockback (when hit by an enemy) [SOLVED]
Post by: maxrostov on November 10, 2020, 03:40:01 PM
Hi.
I've created a state in which the player enters if hit by an enemy.

The state is suppose to make the enemy push the player away from himself.

Currently the player does get pushed back when the enemy attacks from the left.
But when the enemy attacks from the right, the player gets pushed INTO the enemy instead.

I guess i'm using the "Vector3 Invert" in a wrong way.

Does anyone know why that is?

Thank you in advance!  :)
Title: Re: 2D player knockback (when hit by an enemy)
Post by: Thore on November 10, 2020, 06:32:58 PM
Direction * -1 will flip the direction.
Title: Re: 2D player knockback (when hit by an enemy)
Post by: maxrostov on November 11, 2020, 02:12:06 AM
Thanks for replying!

I've tried to set the multiply on a negative number, but that only reversed the problem (player only gets pushed when attacked from right, but player gets drawn into the enemy when attacked from left).

I need the player to get pushed to the opposite direction from where the attack comes from. Isn't that was Vector3 Invert should do in this case?


PS: When I remove Vector3 Invert, nothing changes. So it's obviously not working. Am I using it wrong, or is that action in Playmaker broken?
Title: Re: 2D player knockback (when hit by an enemy)
Post by: maxrostov on November 11, 2020, 02:08:17 PM
Here's a video of the issue (players is only bouncing away from the enemy from one side): https://share.vidyard.com/watch/X2iA7rUt3h5dBiemDnnkkX?

Does anyone know what Action in the FSM that adds force to the opposite direction?
Title: Re: 2D player knockback (when hit by an enemy)
Post by: djaydino on November 12, 2020, 05:18:30 AM
Hi.
You need to get the side where the player is.
if 2d : get pos X from player and enemy then do float compare.

then depending on which side, do negative or positive multiply.
Title: Re: 2D player knockback (when hit by an enemy)
Post by: maxrostov on November 12, 2020, 02:39:29 PM
Hi.
You need to get the side where the player is.
if 2d : get pos X from player and enemy then do float compare.

then depending on which side, do negative or positive multiply.

It worked like a charm - thank you!!

- State1: First I did Collision 2D event. IF collider tag is Enemy, then next state.
- State2: "Get position" of player, and collider. Then float compare of those two variables.
 
(Instead of get the enemy pos, I used get the collider position - because I want this effect to take place when hit by all enemies)

The next two states are either pos or neg multiply.