Simultaneous mouse and keyboard controls for top-down 2D

Godot Version

3.6

Question

` Hey! Making a top-down 2D game where the player can use either their mouse to click a position for their avatar to move to, or their arrow keys to move their avatar. I’m incredibly new to Godot; I started using it a few days ago for this project and it’s for school :'-).

My attempt at doing this is below:`


export (int) var speed = 100
var velocity = Vector2()

onready var target = position
var isMouse = true

func get_input():

	velocity = Vector2()

	#maus

	if Input.is_action_pressed("clicky"):
		target = get_global_mouse_position()
		isMouse = true
		velocity = position.direction_to(target)*speed

		if velocity.x>0:
			$PlayerSprite.flip_h=false
		elif velocity.x<0:
			$PlayerSprite.flip_h=true
	
	#kibbord
	else:
		isMouse = false
		if Input.is_action_pressed("moveRight"):
			velocity.x += 1
			$PlayerSprite.flip_h = false
		if Input.is_action_pressed("moveLeft"):
			velocity.x -= 1
			$PlayerSprite.flip_h = true
		if Input.is_action_pressed("moveDown"):
			velocity.y += 1
		if Input.is_action_pressed("moveUp"):
			velocity.y -= 1
		velocity = velocity.normalized()*speed
	

func _physics_process(delta):
	get_input()
	if velocity.distance_to(target)>5:
		velocity = move_and_slide(velocity)
		for i in get_slide_count():
			var collision = get_slide_collision(i)
			print("I collided with ", collision.collider.name)
	else:
		velocity = move_and_collide(velocity)
		var collision = move_and_collide(velocity * delta)
		if collision:
			print('coll')

func _ready():
	pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
#	pass

`This code is the amalgamation of the example in the documentation and collision functionality that I need to be able to change scenes later; unrelated! :'-)

Currently, the user has to hold down mouse click to move their character. I want to implement it in a way that allows the user to click a position that their avatar would smoothly ease from its starting position to the position clicked unless they press one of the arrow keys, in which case their avatar moves according to the arrow key pressed and the previous easing movement is aborted, if that makes sense.

Thanks in advance!

P.S. If you guys could point me in the right direction in terms of tutorials or any useful tips for Godot development, it would be awesome.

P.P.S. I need to use Godot 3 for this project as I need Web Export functionality for when I finish the project.`

Hi, see below my attempt at rewriting your code within the get_input() function so that it works as you intended. I tried to preserve as much of your original code as possible. I tested on 4.x Godot, but should work on 3.x too. Let me know if you have any issues with that.

func get_input():
	var movement_direction = Vector2()
	
	if Input.is_action_pressed("moveRight"):
		movement_direction.x += 1
	if Input.is_action_pressed("moveLeft"):
		movement_direction.x -= 1
	if Input.is_action_pressed("moveDown"):
		movement_direction.y += 1
	if Input.is_action_pressed("moveUp"):
		movement_direction.y -= 1
	
	if movement_direction != Vector2():
		isMouse = false
	elif Input.is_action_just_pressed("clicky"):
		target = get_global_mouse_position()
		isMouse = true
		movement_direction = position.direction_to(target)
	elif isMouse:
		movement_direction = position.direction_to(target)
	
	velocity = movement_direction.normalized()*speed
	if velocity.x>0:
		$PlayerSprite.flip_h=false
	elif velocity.x<0:
		$PlayerSprite.flip_h=true
1 Like