Today I make enemies get pushed back or knocked up when shot by the player.
This is what I did today:
In the Enemy Script
First, create a Vector3 variable named enemy_pos.

Next, in the Start() function, set enemy_pos = transform.position to store the object’s position. And print the enemy’s position after it gets shot in OncollisionEnter2D() function. This will help me to track the enemy’s position.


And it returns the enemy’s position after it gets shot.

Create a Vector2 variable called KBDirection and set it to (transform.position – other.transform.position).normalized. Then, define KBForce as 5f. Finally, apply the knock back by setting rb.velocity = KBDirection * KBForce.

But after running the code, it didn’t work. I’ll try to fix it next time.
No Responses