Godot Version
v4.2.2
Question
I’m trying to make a simple stacking game. When one ingredient falls and the mouse is clicked, it’s supposed to fall quicker. However, more than one instance at a time falls when it receives the mouse input.
I have tried to use ray casting so that the ingredient that is the closest to the bread will fall quicker when clicked but it only works on the first ingredient.
Send help. All help is appreciated
without raycasting
#stack.gd - without raycasting:
extends Node
@onready var cheese_scene = preload("res://objects/cheese.tscn")
@onready var timer = $Timer
# Constants
const NEW_CHEESE_POSITION = Vector3(0, 6.06, 0) # Position to spawn new cheese
var cheese_queue = [] # Queue to store cheese instances
func _ready():
timer.connect("timeout", Callable(self, "_on_timer_timeout"))
timer.start()
func _on_timer_timeout():
spawn_new_cheese()
func spawn_new_cheese():
var new_cheese = cheese_scene.instantiate()
new_cheese.global_transform.origin = NEW_CHEESE_POSITION
add_child(new_cheese)
cheese_queue.append(new_cheese)
new_cheese.connect("cheese_fell", Callable(self, "_on_cheese_fell"))
update_cheese_clickability()
func update_cheese_clickability():
for cheese in cheese_queue:
cheese.can_be_clicked = false # Disable all cheeses from being clicked
if cheese_queue.size() > 0:
cheese_queue[0].can_be_clicked = true # Enable the oldest cheese to be clicked
func _on_cheese_fell():
if cheese_queue.size() > 0:
cheese_queue.pop_front() # Remove the oldest cheese from the queue
update_cheese_clickability()
#cheese.gd
extends RigidBody3D
# Constants
const INITIAL_GRAVITY = Vector3(0, -0.5, 0) # Even slower initial gravity
const FAST_GRAVITY = Vector3(0, -30.0, 0) # Fast gravity when clicked
var initialGravitySet = false # Flag to track if initial gravity is set
var fastFall = false # Flag to track if fast falling is enabled
var can_be_clicked = false # Flag to track if this cheese can be clicked
signal cheese_fell # Signal to notify when the cheese falls fast
# Called when the node enters the scene tree for the first time.
func _ready():
set_physics_process(false) # Disable physics process until falling starts
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _physics_process(delta):
if initialGravitySet:
if fastFall:
# Apply fast gravity
linear_velocity += FAST_GRAVITY * delta
else:
# Apply initial slow gravity
linear_velocity += INITIAL_GRAVITY * delta
# Handle mouse input
func _input(event):
if can_be_clicked and event is InputEventMouseButton:
var mouse_event = event as InputEventMouseButton
if mouse_event.button_index == MOUSE_BUTTON_LEFT and mouse_event.pressed:
if not initialGravitySet:
set_initial_gravity()
enable_fast_fall()
# Set initial slow falling speed
func set_initial_gravity():
initialGravitySet = true
set_physics_process(true) # Enable physics process to update falling
# Enable fast falling speed
func enable_fast_fall():
fastFall = true
emit_signal("cheese_fell") # Emit signal when the cheese falls fast
#timer.gd:
#not needed
extends Timer
func _ready():
wait_time = 0.5
start()