rigidbody2d object freeze when i move camera

Godot Version

godot 4

Question

hello, i am new here and to godot in general. what im trying to use ai (im not good with programming but im slowly learnig from it) to make a rigid body2d follow mouse and rotate with a or d and go loose when left clicked, howevern whenever i move the camera, the bodies freezes when moving or falling. they act just fine when they fall from mouse but not once colliding with the first block or tile it hits. here is the code im referring:

extends RigidBody2D

var is_falling = false
var has_collided = false # Flag to ensure sound plays only once per collision
var sound_files = [
preload(“res://AGMaker/sounds/shapes/Concrete01.wav”),
preload(“res://AGMaker/sounds/shapes/Concrete02.wav”),
preload(“res://AGMaker/sounds/shapes/Concrete03.wav”),
preload(“res://AGMaker/sounds/shapes/Concrete04.wav”),
preload(“res://AGMaker/sounds/shapes/Concrete05.wav”),
preload(“res://AGMaker/sounds/shapes/Concrete06.wav”),
preload(“res://AGMaker/sounds/shapes/Concrete07.wav”),
preload(“res://AGMaker/sounds/shapes/Concrete08.wav”)
]

@onready var audio_player = $AudioStreamPlayer2D

func _ready():
gravity_scale = 0.0
contact_monitor = false
max_contacts_reported = 1
set_collision_layer_value(1, false)
set_collision_mask_value(1, false)
linear_velocity = Vector2.ZERO
angular_velocity = 0.0
freeze = true
body_entered.connect(_on_body_entered)
print(“AudioStreamPlayer2D node:”, audio_player) # Debug: Check node

func _physics_process(delta):
if not is_falling:
var mouse_pos = get_global_mouse_position()
global_position = mouse_pos
if Input.is_key_pressed(KEY_A):
rotation_degrees -= 180 * delta
if Input.is_key_pressed(KEY_D):
rotation_degrees += 180 * delta

func _input(event):
if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT and event.pressed:
if not is_falling:
is_falling = true
freeze = false
gravity_scale = 0.5
contact_monitor = true
max_contacts_reported = 1
set_collision_layer_value(1, true)
set_collision_mask_value(1, true)
linear_velocity = Vector2.ZERO
angular_velocity = 0.0
set_process_input(false)
print(“Block released, falling”) # Debug: Confirm falling

func _on_body_entered(_body: Node):
print(“Collision detected with:”, _body.name, “Velocity:”, linear_velocity.length(), “Rotation:”, rotation_degrees)
if is_falling and not has_collided:
has_collided = true
_play_random_sound()
await get_tree().create_timer(0.5).timeout
has_collided = false

func _play_random_sound():
print(“Playing sound, stream:”, sound_files[randi() % sound_files.size()].resource_path)
var random_index = randi() % sound_files.size()
audio_player.stream = sound_files[random_index]
audio_player.play()

Please paste all multiline code between sets of three ticks (```) to make it readable. Putting “gdscript” or “gd” behind the first set of ticks improves the highlighting for GDScript code:

```gd
code
```

Without the indentation it’s not possible to know exactly what the code does and where the problem is.

1 Like