Godot Version
v4.6.1
Question
It is supposed to put the “bugs” in that area and make them bounce around like billiard balls, instead they do that.
Below Iha
If you share code, please wrap it inside three backticks or replace the code in the next block:
extends Node
var minigames = [
"Bug"
]
var score = 0
signal done
@onready var bug = preload("res://bug.tscn")
func _on_minigame_button_minigamepressed() -> void:
var gamerand = minigames.pick_random()
if gamerand == "Bug":
for i in 10:
var cs2d = $"/root/Node2D/Minigames/Area2D/CollisionShape2D"
var buginstance = bug.instantiate()
buginstance.connect("killed", morescore)
buginstance.visible = true
var shape = cs2d.shape as RectangleShape2D
var spawnrange = shape.extents
var random_x = randf_range(-spawnrange.x, spawnrange.x)
var random_y = randf_range(-spawnrange.y, spawnrange.y)
var spawnpos = cs2d.global_position + Vector2(random_x, random_y)
buginstance.global_position = spawnpos
get_parent().add_child(buginstance)
set_meta("bugsalive", get_meta("bugsalive") + 1)
await get_tree().create_timer(1).timeout
extends RigidBody2D
signal killed
func _ready() -> void:
impulse()
func impulse():
var impulsex = randf_range(-20, 20)
var impulsey = randf_range(-20, 20)
apply_central_impulse(Vector2(impulsex, impulsey))
func _on_area_2d_unhandled_input_event(viewport: Node, event: InputEvent, shape_idx: int) -> void:
if event is InputEventMouseButton:
if event.pressed and event.button_index == MOUSE_BUTTON_LEFT:
emit_signal("killed")
queue_free()
What are the bugs doing right now?
Some thoughts:
1/ Your impulse might be too small depending on your ball properties. What happens if you change randf_range(-20, 20) to like randf_range(-2000, 2000)?
2/ Your code is generating Area2Ds. These don’t have physics, they’re used for like detections. I’d recommend learning more - read about StaticBody2D (StaticBody2D — Godot Engine (stable) documentation in English), it’s probably what you want
3/ Both the bug and your StaticBody2D wall (when you switch to StaticBody2D) need to have a PhysicsMaterial with a bounce set to 1 for the bounce to work, otherwise it’ll just collide and stop (I think).
What do you mean? What do they do instead?
What is in your console, errors, warnings, anything that can help pinpoint the issue?
i uh.. tried to send a video and thought it uploaded but it did not and i couldnt delete the post
You can only upload videos when you reach a certain trust level.
Try pasting the errors from the console if any, and describe what happens the best you can;
there isnt much else i can do otherwise.
There is no errors, but they all just basically spawn in and fly straight into a corner and not in the designated spawn area.
Do you have gravity causing issues? Try buginstance.gravity_scale = 0
Also
buginstance.global_position = spawnpos
get_parent().add_child(buginstance)
this looks sus? Idk what happens if you set the global position before adding it as a child, it might cause it to offset again. Try doing
get_parent().add_child(buginstance)
buginstance.global_position = spawnpos
But tbh we’re just out here making educated guesses because we don’t see the full project
I Will try that once I’m at my computer, thank you for your help. And if it helps there is a post on r/godot with the same name that has the video I was going to use.
Best thing I’ve read today.