1
\$\begingroup\$

i made a godot game in which theres a player and a drone, I got the shooting to work but when i tried to make it so that the bullet attacks the drone/player, it wasnt working properly. Basically i made a area2d in the bullet and if a body enters it, i damage the body (player/drone). or if the body is a wall then i destroy the bullet. When i tried debugging body.name, all i got was 'Wall' in the console. heres the code for the bullet

extends Node2D

var angle = 0
var speed = 0

func set_bullet_settings(new_angle, new_speed, pos, amountoffset):
    angle = new_angle
    speed = new_speed
    $BulletSprite.rotation_degrees = new_angle  
    $BulletSprite.position = pos
    
    $BulletSprite.position.x += cos(deg_to_rad(-angle)) * amountoffset
    $BulletSprite.position.y -= sin(deg_to_rad(-angle)) * amountoffset
    $AttackArea.position.x += cos(deg_to_rad(-angle + 90)) * amountoffset
    $AttackArea.position.y -= sin(deg_to_rad(-angle + 90)) * amountoffset

func update():
    $BulletSprite.position.x += cos(deg_to_rad(-angle + 90)) * speed
    $BulletSprite.position.y -= sin(deg_to_rad(-angle + 90)) * speed
    $AttackArea.position.x += cos(deg_to_rad(-angle + 90)) * speed
    $AttackArea.position.y -= sin(deg_to_rad(-angle + 90)) * speed


func _on_attack_area_body_entered(body):
    print(body.name)
    if body.name == 'Walls':
        queue_free()

code for player

extends CharacterBody2D

const speed = 5
const fast_speed = 7
const shoot_speed = 0.3
const bullet_speed = 50

var win_mode = 'fullscreen'
var bullets_shooten = []
var bullet_scene = preload('res://scenes/bullet/bullet.tscn')
var angle = 180
var can_shoot = true

func isnt_fast():
    return not Input.is_action_pressed("walk faster")

func _ready():
    $BulletTimer.wait_time = shoot_speed
    $BulletTimer.connect('timeout', set_can_shoot)
    
    DisplayServer.mouse_set_mode(DisplayServer.MOUSE_MODE_HIDDEN)

func set_angle_to_mousepos():
    var direction = (get_global_mouse_position() - self.global_position).normalized()
    angle = int(rad_to_deg(atan2(direction.y, direction.x))) + 90

func set_can_shoot():
    can_shoot = true

func move(amount):
    position.x += amount * cos(deg_to_rad(-angle + 90))
    position.y -= amount * sin(deg_to_rad(-angle + 90))

func shoot():
    var bullet = bullet_scene.instantiate()
    bullet.set_bullet_settings(angle, bullet_speed, position, randi_range(0,16))
    bullets_shooten.append(bullet)
    $'..'.add_child(bullet)
    can_shoot = false
    $BulletTimer.start()

func _physics_process(delta):
    if Input.is_action_pressed("move forward"):
        move(speed  if isnt_fast() else fast_speed)
    if Input.is_action_pressed("move backward"):
        move(-speed if isnt_fast() else -fast_speed)
    if Input.is_action_pressed("shoot") and can_shoot:
        shoot()

    move_and_slide()

func enumerate(iterable):
    var index = 0
    var result = []

    for item in iterable:
        result.append([index, item])
        index += 1

    return result

func removeNullItems(inputArray: Array) -> Array:
    var newArray := []

    for item in inputArray:
        if item != null:
            newArray.append(item)

    return newArray

func _process(delta):
    set_angle_to_mousepos()
    angle = (angle + 360) % 360
    $Sprite.rotation_degrees = angle - 5
    
    if Input.is_action_just_pressed('fullscreen toggle'):
        if win_mode == 'fullscreen':
            DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_FULLSCREEN)
            win_mode = 'not fullscreen'
        else:
            DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_WINDOWED)
            win_mode = 'fullscreen'

    bullets_shooten = removeNullItems(bullets_shooten)


    for bullet in bullets_shooten:
        bullet.update()

code for drone

extends CharacterBody2D

const accuracy = 30

var can_shoot: bool = true
var bullet_scene = preload('res://scenes/bullet/bullet.tscn')
var shoot_speed = 0.7
var bullet_speed = 35
var bullets_shooten = []
var player_in_range = false

func _ready():
    $BulletTimer.wait_time = shoot_speed
    $BulletTimer.connect('timeout', set_can_shoot)

func set_can_shoot():
    can_shoot = true

func shoot(playerpos):
    var bullet = bullet_scene.instantiate()
    bullet.set_bullet_settings(rotation_degrees, bullet_speed, position, randi_range(0,accuracy))
    bullets_shooten.append(bullet)
    $'..'.add_child(bullet)
    can_shoot = false
    $BulletTimer.start()

func look_at_player(player_pos):
    look_at(player_pos)
    rotation_degrees += 90

func removeNullItems(inputArray: Array) -> Array:
    var newArray := []

    for item in inputArray:
        if item != null:
            newArray.append(item)

    return newArray

func update(player_pos):
    look_at_player(player_pos)
    if player_in_range and can_shoot:
        shoot(player_pos)

    bullets_shooten = removeNullItems(bullets_shooten)
    
    for bullet in bullets_shooten:
        bullet.update()





func _on_attack_range_body_entered(body):
    if body.name == 'Player':
        player_in_range = true

func _on_attack_range_body_exited(body):
    if body.name == 'Player':
        player_in_range = false


```
\$\endgroup\$
2
  • \$\begingroup\$ Not really a Godot user, but where exactly do you handle colliision of your bullet with either player or drone? All you have is one body.name == 'Walls'. \$\endgroup\$
    – Zibelas
    Commented Aug 16, 2023 at 10:54
  • \$\begingroup\$ actually, i deleted that because i was debugging. The problem is that theres only 'Walls' showing in the console so that means for some reason the bullet isnt colliding with the drone/player \$\endgroup\$ Commented Aug 16, 2023 at 11:06

1 Answer 1

1
\$\begingroup\$

I don't have much to see in the code...

What I can give you is a short list of things to check to debug this:

  • Make sure your Area2D and CharacterBody2D have their colliders (with their shape, and not disabled). Double check by setting "Visible Collision Objects" in the debug menu, which will show the colliders when the game is running. This will also reveal if the colliders are positioned or moving in an unexpected way.
  • Make sure the collision layer and mask of the Area2D and CharacterBody2D overlap, otherwise, Godot won't even check for a collision between them.
  • Make sure the body_entered signal is connected correctly. I'm guessing it is connected since you say you got the output from print. I presume you are connecting it from the editor (since the connection is not being made in the code you shared), in which case you should see in Godot text editor a green icon next to the method connected to the signal, and clicking it should show you what is connected.
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .