Recasting Problems

Godot Version

Latest Version Of Godot (4.2 I think)

Question

I am trying to make a brotato type game and the raycast on the gun only registers its hitting something when im shooting the enemy from a specific angle. I included a picture to display the angle.

Can you paste your script?

```
type or paste code here
```

Any ideas why this might be happening? are your enemies collision shapes scaled non-uniformly?

the collision shapes are uniform

raycast code
(I am very new at coding and Godot)

extends RayCast2D

@onready var ray_cast = $"."
@onready var timer = $Timer


func _input(event):
	if Input.is_action_just_pressed("fire"):
		ray_cast.enabled = true
		timer.start()


func _on_timer_timeout():
	ray_cast.enabled = false

func _physics_process(delta):
	if ray_cast.enabled == true:
		if ray_cast.is_colliding():
			print ("i hit")

gun code

extends Sprite2D

@onready var gun = $"."
@onready var animated_sprite = $"../AnimatedSprite2D"
@onready var ray_cast = $RayCast2D

func _physics_process(delta):
	#rotate gun to look at mouse
	look_at(to_global(-get_local_mouse_position()))
	ray_cast.force_raycast_update()
	if gun.rotation_degrees > 90:
		if gun.rotation_degrees > 270:
			flip_v = false
		else:
			flip_v = true
	
	
	if gun.rotation_degrees > 359 or gun.rotation_degrees < -359:
		gun.rotation_degrees = 0


it may be happening because of the gun rotation but idk

I don’t think it’s the rotation, is the orange line a drawing? Try playing with visible collision shapes on. To turn it on go to the “Debug” menu


I’ve seen this a lot, is there a particular tutorial telling people to use this line? It’s totally unnecessary. You can get the current node with self or nothing, it implicitly looks in the node’s properties and methods automatically.

# all three of these lines are the same
# you should prefer the last two for performance
if gun.rotation_degrees > 90:
if self.rotation_degrees > 90:
if rotation_degrees > 90:

I turned on visible collisions and the raycast only appears when im pointing at my self. if the gun is not aiming at the player and the ray is not touching the player than the ray goes away

Oh I figured it out the rays collision mask was 2 and the enemys collision layer was 1

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.