Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Pikasalt |
I’ve been following UmaiPixel’s tutorial on making a 2d platfomer, and I was wondering how I would go about making an enemy get knocked back by a projectile similar to how it is done in Kirby games? I’ve tried following forncake’s tutorial on enemy knockback, but I’m not sure how to implement it in a 2D platfomer
This is the code for the enemy:
extends KinematicBody2D
const GRAVITY = 10
const SPEED = 20
const FLOOR = Vector2(0, -1)
var velocity = Vector2()
var direction = 1
var is_dead = false
func _ready():
pass
func dead():
is_dead = true
velocity = Vector2(0, 0)
$AnimatedSprite.play("ded")
$CollisionShape2D.disabled = true
$Timer.start()
func _physics_process(_delta):
velocity.y += GRAVITY
if is_dead == false:
velocity.x = SPEED * direction
if direction == 1:
$AnimatedSprite.flip_h = false
else:
$AnimatedSprite.flip_h = true
$AnimatedSprite.play("snakewalk")
velocity = move_and_slide(velocity, FLOOR)
if is_on_wall():
direction = direction * -1
$RayCast2D.position.x *= -1
if $RayCast2D.is_colliding() == false:
direction = direction * -1
$RayCast2D.position.x *= -1
func _on_Timer_timeout():
queue_free()
And this is the code for the projectile:
extends Area2D
const SPEED = 100
var velocity = Vector2()
var direction = 1
func _ready():
pass
func set_fireball_direction(dir):
direction = dir
if dir == 1:
$Sprite.flip_h = true
$CPUParticles2D.gravity.x = -40
if dir == -1:
$Sprite.flip_h = false
$CPUParticles2D.gravity.x = 40
func _physics_process(delta):
velocity.x = SPEED * delta * direction
translate(velocity)
func _on_VisibilityNotifier2D_screen_exited():
queue_free()
func _on_Fireball_body_entered(body):
if "Snake" in body.name:
body.dead()
queue_free()