Hi, I am trying to create a player movement system where the player moves around by mouse interaction. The player follows the mouse and stops when the left button is clicked.
I managed to get the script to detect the button is being click, held, and released.
However, my main issue is trying to create a dash function where the player holds down the left click button and upon release the player dashes to the mouse’s position. Any way I could achieve this?
Here is my current code:
extends CharacterBody2D
@export var speed = 200
var mouse_position = null
var release_position = Vector2()
var is_moving_enabled = false
func _ready():
release_position = position
func _physics_process(_delta):
if is_moving_enabled:
mouse_position = get_global_mouse_position()
var direction = (mouse_position - position).normalized()
velocity = (direction * speed)
move_and_slide()
look_at(mouse_position)
else:
velocity = Vector2(0,0)
func _input(event):
if event is InputEventMouseButton:
if event.button_index == MOUSE_BUTTON_LEFT and event.pressed:
print("Left mouse button clicked")
is_moving_enabled = !is_moving_enabled
if Input.is_action_pressed("left_click"):
print("Left mouse button held")
is_moving_enabled < is_moving_enabled
if Input.is_action_just_released("left_click"):
print("release")
var time = 0
func _input(event):
if event is InputMouseButton and event.button_index == MBL:
if event.pressed: # button is pressed
#start timer to identify if it is a long or short press
time = Time.get_ticks_msec()
else: #button is released
#check if time passed long enaugh
#make a dash, else just move
#reset timer
time = 0
I added a timer node and placed a timeout function to the code. However, I’m not sure if I’m going in the right direction
This is my code so far:
if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT:
if event.pressed: # button is pressed
timer.start(3) #start timer to identify if it is a long or short press
time = Time.get_ticks_msec()
print("pressed time achieved")
else: #button is released
print("release")
time = timer.start #check if time passed long enough
mouse_position = get_global_mouse_position()
position.distance_to(release_position) #make a dash, else just move
release_position = (mouse_position - position).normalized()
velocity = release_position * speed
move_and_slide()
time = 0 #reset timer
func _on_timer_timeout() -> void:
queue_free()
The release is still detected. However, instead of playing the print statement “pressed time achieved” upon left_click pressed down and moving to the mouse’s position, when the timer passes the player’s sprite disappears off screen. Any idea what I could change?