Godot Version
4.2.2
Question
Im having trouble with raycast2D in script not detecting hits.
Heres my script for the raycast2Ds
extends Node2D
#Gun Stats
var Damage : int = 10
var KnockbackForce : float = 10
var ReloadTime : float = 1 # secs
var Ammo : float = 3
var AttackSpeed : float = 0.5 # secs
var BulletsPerFire : float = 2 # amount of bullets that come out per click
var BulletRange : float = 300 # how far a bullet raycast goes before ending
var AutoFire : bool = true
var CanFire : bool = true
func _process(_delta):
if AutoFire == true && Input.is_action_pressed("Fire") && CanFire == true:
for index in BulletsPerFire:
shootGun()
if AutoFire == false && Input.is_action_just_pressed("Fire") && CanFire == true:
for index in BulletsPerFire:
shootGun()
rotateGun()
func shootGun():
CanFire = false
var ray = RayCast2D.new()
ray.exclude_parent = true
ray.collision_mask = 2
ray.position = $WeaponPivot/BulletSpawnPoint.position
ray.target_position.y = randf_range(-5, 5)
ray.target_position.x = BulletRange
ray.rotation = $WeaponPivot.rotation
ray.enabled = true
#shows raycast debug line
var line = Line2D.new()
line.width = 1
line.add_point(ray.position)
line.add_point(ray.target_position)
line.rotation = $WeaponPivot.rotation
#
#shows debug startpos and endpos for raycast
$WeaponPivot/StartPos.position = ray.position
$WeaponPivot/EndPos.position = ray.target_position
#
add_child(line)
add_child(ray)
if ray.is_colliding():
print("hit")
cockGun()
await get_tree().create_timer(0.1).timeout
ray.queue_free()
line.queue_free()
func rotateGun():
$WeaponPivot.look_at(get_global_mouse_position())
if get_local_mouse_position().x < 0:
$WeaponPivot/BulletSpawnPoint.position = Vector2(30, 2)
$WeaponPivot/GunSprite2D.flip_v = true
else :
$WeaponPivot/BulletSpawnPoint.position = Vector2(30, -2)
$WeaponPivot/GunSprite2D.flip_v = false
func cockGun():
await get_tree().create_timer(AttackSpeed).timeout
CanFire = true
the var ray is suppose to check if it hits in the layer mash “2” after it is added to the scene, but it doesn’t print.
Test Enemy Layout
Test Enemy Collision
Raycasts are added to Gun node2d as a child