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.`