Projectile that bounces off walls

I’m trying to make a projectile that bounces off walls on a 2D Top Down project. How can I make one?
My current code:
extends Area2D

var speed = 200

var dir : Vector2
func _physics_process(delta):
translate((speed*dir)*delta)

func _on_visible_on_screen_enabler_2d_screen_exited():
queue_free()

I am just taking a stab in the dark but maybe something akin to

if raycast.is_colliding():
rotation = rotation.reflect(ray.get_collision_normal())

I Think also in the Vector2 class has a “Bounce” attribute you can work with.

2 Likes

When i tried putting the code on the Script it gave the “Unexpected “if” in class body.” and Unexpected “Identifier” in class body.

They way I have added a raycast to my platformer was add a script to my enemy itself as his of entity.

I have a onready var for the raycast which for my purpose I renamed groundcast. I also have a true/false exported var called checks_ground, that if it doesn’t detect the ground it flips them. If it doesn’t check the ground it just plummets to the next platform. Perhaps you could do some changes to make it work as a reflect or bounce calculation.

func _ready():
	if direction == 1:
		$Enemy/EnemyAni.play("enemy_move")
		$GroundCast.position.x = $CollisionShape2D.shape.size.x/2 * direction + $CollisionShape2D.position.x
		$GroundCast.enabled = checks_ground
	
	
func _physics_process(_delta):
	# Makes enemy check GroundCast to patrol platforms
	if is_on_wall() or not $GroundCast.is_colliding() and checks_ground and is_on_floor():
		direction = direction * -1
		$Enemy.flip_h = not $Enemy.flip_h
		$GroundCast.position.x = $CollisionShape2D.shape.size.x/2 * direction + $CollisionShape2D.position.x
		print("turn around!")
1 Like