Godot Version
4.3
Question
Help Programming Physics
I’m trying to make a drag and drop puzzle game, and when I drop my block, there is a specific spot (the spot in which I spawn the block at the start of the scene) where there is nothing but the block still collides with it.
This is the full script of the block:
extends RigidBody2D
var draggable:bool = true
var mouse_pos:Vector2
var dragging:bool = false
var sprite:Sprite2D
@onready var actualScale:Vector2 = $sprite.scale
@onready var camera:Camera2D = get_parent().get_parent().get_node("Camera2D")
var MAX_SHAKE:float = 3
const HOVER_RESCALE:float = 1.1
func _physics_process(_delta: float) -> void:
if draggable: #Draggability
$center.global_position = global_position #resets the updated position of the sprite
if dragging and Input.is_action_pressed("click"):
$sprite.modulate = "ffffff50" #changes to alpha to transparent
if sprite == null: #If there is no duplicate of the current texture
sprite = Sprite2D.new()
sprite.texture = load($sprite.texture.resource_path)
add_child(sprite)
else: #If the sprite already exists
sprite.scale = actualScale*HOVER_RESCALE
sprite.global_position = mouse_pos #Follow the cursor without snap
sprite.rotation_degrees = lerp(sprite.rotation_degrees, MAX_SHAKE, 0.25) #Shakes the block while being held
if abs(sprite.rotation_degrees) > abs(MAX_SHAKE)-0.5: MAX_SHAKE = -MAX_SHAKE #When the shake reached the limit, it rotates back the other direction
$sprite.global_position = snapped(mouse_pos,Vector2(16,16)) #The transparent sprite follows with snap
$center.global_position -= Vector2(0,1000)
elif dragging and Input.is_action_just_released("click"):
sprite.scale = actualScale
$sprite.modulate = "ffffff" #changes the alpha to opaque
dragging = false
global_position = $sprite.global_position #Resets the positions of the sprites
$sprite.global_position = global_position
if sprite != null: remove_child(sprite) #Removes the fake sprite
sprite = null
func _input(event: InputEvent) -> void:
if draggable: #Draggability
if event is InputEventMouseMotion:
#NEVER CHANGE THE FORMULA OR THE DRAGGING STOPS WORKING
mouse_pos = event.global_position/camera.zoom + camera.global_position - (camera.get_viewport_rect().size/2/camera.zoom)
func _on_center_mouse_entered() -> void:
if draggable: #Draggability
$sprite.scale = actualScale*HOVER_RESCALE
if not Input.is_action_pressed("click"):
dragging = true
func _on_center_mouse_exited() -> void:
if draggable: #Draggability
$sprite.scale = actualScale
if not Input.is_action_pressed("click"):
dragging = false
I have no idea to why this happens nor how to fix it.
Any help is appreciated, thanks in advance.