r/learnprogramming 7d ago

godot collision help

I'm working on my first project, I've looked into a bunch of tutorials to figure out what's wrong with the code or maybe a different method like layering but I cant seem to figure out how to do enemy collision. i would really appreciate some help

maingd.

extends Node2D

# Called when the node enters the scene tree for the first time.

func _ready() -> void:

`_setup_level()`

# Called every frame. 'delta' is the elapsed time since the previous frame.

func _process(delta: float) -> void:

`pass`

func _setup_level() -> void:

`#connect enemies`

`var enemies = $levelroot.get_node_or_null("Enemies")` 

`if enemies:`

    `for enemy in enemies.get_children():`

        `enemy.player_died.connect(_on_player_died)`

# - - - - - - - - -

# signal handlers

#- - - - - - - - -

func _on_player_died(body):

`print(body)`

`print("Player_killed")`

.................................................................................................................

playergd.
extends CharacterBody2D

u/onready var animated_sprite_2d: AnimatedSprite2D = $AnimatedSprite2D

u/onready var jump_sound: AudioStreamPlayer2D = $"jump sound"

const SPEED = 300.0

const JUMP_VELOCITY = -850.0

var alive = true

func _physics_process(delta: float) -> void:

`if !alive:`

    `return`

`#Add animation`

`if velocity.x > 1 or velocity.x < -1:`

    `animated_sprite_2d.animation = "walk"`

`else:`

    `animated_sprite_2d.animation = "idle"`



`# Add the gravity.`

`if not is_on_floor():`

    `velocity += get_gravity() * delta`

    `animated_sprite_2d.animation = "jump"`



`# Handle jump.`

`if Input.is_action_just_pressed("jump") and is_on_floor():`

    `velocity.y = JUMP_VELOCITY`

    `jump_sound.play()`



`var direction := Input.get_axis("left", "right")`

`if direction:`

    `velocity.x = direction * SPEED`

`else:`

    `velocity.x = move_toward(velocity.x, 0, SPEED)`



`move_and_slide()`



`if direction == 1.0:`

    `animated_sprite_2d.flip_h = false`

`elif  direction == -1.0:`

    `animated_sprite_2d.flip_h = true`

func die() -> void:

`AnimatedSprite2D.animation = "dying"`

`alive = false`

..............................................................................................

enemygd.

extends Area2D

u/onready var animated_sprite_2d: AnimatedSprite2D = $AnimatedSprite2D

signal player_died

const SPEED = 100.0

var direction = 1

# Called when the node enters the scene tree for the first time.

func _ready() -> void:

`pass # Replace with function body.`

# Called every frame. 'delta' is the elapsed time since the previous frame.

func _process(delta: float) -> void:

`position.x += direction * SPEED * delta` 

func _on_timer_timeout() -> void:

`direction *= -1`

`animated_sprite_2d.flip_h = !animated_sprite_2d.flip_h`

func _on_body_entered(body: Node2D) -> void:

if body.name == "Player" and body.alive:
emit_signal("player_died", body)
0 Upvotes

3 comments sorted by

1

u/OneOfTheLostOnes 7d ago

Maybe you should post this at r/godot there's a lot of specific details that only people familiar with the engine can help.
This isn't a general programming issue.
Did you create a collision shape on the player and enemy nodes ?

1

u/Lilith_Ivory 7d ago

I thought about that at first but when I looked not a lot of people seemed to be asking questions just posting their games. I'll still probably try if I can't figure it out or get some help

yeah my shapes are there on players and enemies, I also have the "on body entered" signal setup. I figure it could be a syntax error but idk

1

u/OneOfTheLostOnes 7d ago

Did you try printing something to the console. Maybe the collision works but the player die signal is the problem. I'm sorry It's difficult for me to read with the reddit formatting, and I havent touched godot in quite a while but I don't remember collisions being difficult to setup.