#Mouse dragging every object in its path with it, when I only want one to be dragged
1 messages · Page 1 of 1 (latest)
If I hold the object and put my mouse over another object while holding left click, it brings that one too with it
I just want one object only to be able to follow the mouse, not others
if anyone needs it here's the entire code
extends CharacterBody2D
@onready var animated_sprite_2d: AnimatedSprite2D = $AnimatedSprite2D
@onready var ray_cast_left: RayCast2D = $RayCastLeft
@onready var ray_cast_right: RayCast2D = $RayCastRight
var mouse_in_range = false
var walk_where = 0
var holding = false
var walk_speed = 10
var not_yet = false
func _ready():
animated_sprite_2d.play("idle")
func _physics_process(delta: float) -> void:
if $walk_time.is_stopped():
$walk_time.start()
print(walk_where)
velocity.y += 5
move_and_slide()
walk()
if mouse_in_range == true:
if Input.is_action_just_pressed("left_click"):
Global.is_mouse_busy = true
if Global.is_mouse_busy == true:
position = get_global_mouse_position()
velocity.y = 0
if Input.is_action_just_released("left_click"):
Global.is_mouse_busy = false
var current_dir = "none"
func walk_wait():
walk_where = randi()%3 + 1
func walk():
if walk_where == 1:
velocity.x = -walk_speed
animated_sprite_2d.play("walk")
animated_sprite_2d.flip_h = false
current_dir = "left"
if walk_where == 2:
velocity.x = walk_speed
animated_sprite_2d.play("walk")
animated_sprite_2d.flip_h = true
current_dir = "right"
if walk_where == 3:
velocity.x = 0
animated_sprite_2d.play("idle")
if current_dir == "right":
animated_sprite_2d.flip_h
func _on_walk_time_timeout() -> void:
walk_wait()
var mouse_distance = Vector2(0, 0)
func _on_hitbox_mouse_entered() -> void:
mouse_distance = position - get_global_mouse_position()
mouse_in_range = true
func _on_left_area_mouse_exited() -> void:
mouse_in_range = false
Global.is_mouse_busy = false
and yeah here the global script JUST incase...
Because once the mouse is set to be busy (globally in fact). Then the condition for dragging is also enabled globally.
You should use a local variable for deciding if the dragging is allowed.
Or maybe even set a global variable that says WHICH object is being dragged, and only allow it if said value is null OR the same as the one clicked.
The later is so if you are holding down on object A, keep allowing the drag.
I tried making it so that the body can only be dragged if the mouse isnt busy, but then whenever I try to pick something up it just only does it for a split second then releases against immediately
Like this
Then whenever i try picking something up, it only does for a split second then releases again
Thing is, rn im testing it on the same object duplicated many times, I want to be able to throw objects around in my game
If it was another object's script I'd probably know what to do, but now its all the same exact script on every single object
So I have no idea what to do