- Edited
I currently make a top down game that has a circle player that eats circle food. I made a score label that says how many I've eaten. Sometimes the circle eats more than 1 food at the same time and the game addresses it as one.
player.gd
extends CharacterBody2D
@onready var you_died = $YouDied
@onready var score = $CanvasLayer/Score
@onready var player = $"."
const weight = 0.2
const SPEED = 450
var kills = 0
func _physics_process(delta):
var input_direction = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
velocity = input_direction * SPEED
move_and_slide()
func _on_area_2d_body_entered(body):
var player_scale = player.scale.x
var food_scale = body.scale.x
print("[Player]:Body name : ",body.name)
print("[Player]: " + str(food_scale) + " " + str(player_scale))
if food_scale > player_scale:
print("[Player]:Died...")
else:
kills += 1
print("Check kills:",kills)
score.text = str(kills)
scale.x += weight
scale.y += weight
food.gd
extends CharacterBody2D
const SPEED = 60
var rng = RandomNumberGenerator.new()
var food_scale
#behaviour of food
var direction_x
var direction_y
var weight = 0.2
@onready var timer = $Timer
func _ready():
var scale_choice = rng.randi_range(0,4)
scale.x = (scale_choice * weight) + 1
scale.y = (scale_choice * weight) + 1
food_scale = scale.x
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _physics_process(delta):
var input_direction = Vector2(rng.randi_range(-1,1), rng.randi_range(-1,1))
velocity = input_direction * SPEED
move_and_slide()
@onready var player = $"../../Player"
func _on_area_2d_area_entered(area):
var player_scale = player.scale.x
print("[Food]:Player : " + str(player_scale) + " Food : " + str(food_scale) + "!")
if player_scale < food_scale:
print("[Food]:Player Got Killed")
get_tree().reload_current_scene()
else:
queue_free()
I think the problem has to do with the fact that the signal is area_2d enter. Any help?
Here is the project :