- Edited
Hello there,
i want to create player charakter related controls ***in 2D*** where the player can move it's charakter by a combination of mouse and button input in the game world. The game prototype is an isometric game. I was following the tutorials of stefan_gamedev on youtube and my charakter can now be moved by clicking the left mouse button or alternatively by pressing the wasd keys to a certain destination. For the game i want to have two control modes for the charakter. The first mode shall be like in Ultima 8. In Ultima 8 one can control the Avatar by holding the left mouse button and the Avatar was running then in that direction the mouse pointer was pointing to. The second mode is intended as a combat mode. The player shall be able to view in any direction depending on the mouse location but at the same time the wasd buttons should be used for movement. So that a player can face it's enemies at the same time as he is moving backwards. So i was thinking about how i can do this but to be honest i don't have a clue. Maybe i should post the code first:
extends KinematicBody2D
onready var animation_tree = get_node("AnimationTree")
onready var animation_mode = animation_tree.get("parameters/playback")
var max_speed = 200
var speed = 0
var acceleration = 750
var moving = false
var destination = Vector2()
var movement = Vector2()
var move_direction = Vector2()
func _unhandled_input(event):
if event.is_action_pressed("Click"):
moving = true
destination = get_global_mouse_position()
print(position.direction_to(get_global_mouse_position()).normalized())
func _process(delta):
pass
func _physics_process(delta):
# ButtonMovementLoop(delta)
# MouseMovementLoop(delta)
get_input(delta)
func ButtonMovementLoop(delta):
move_direction.x = int(Input.is_action_pressed("Right")) - int(Input.is_action_pressed("Left"))
move_direction.y = (int(Input.is_action_pressed("Down")) - int(Input.is_action_pressed("Up"))) / float(2)
if move_direction == Vector2(0, 0):
speed = 0
animation_mode.travel("Idle")
else:
speed += acceleration #* delta
if speed > max_speed:
speed = max_speed
var movement = move_direction.normalized() * speed
move_and_slide(movement)
animation_tree.set("parameters/Walk/blend_position", movement.normalized())
animation_tree.set("parameters/Idle/blend_position", movement.normalized())
animation_mode.travel("Walk")
func get_input(delta):
var velocity = Vector2.ZERO
if Input.is_action_pressed("Right"):
velocity.x += 1
if Input.is_action_pressed("Left"):
velocity.x -= 1
if Input.is_action_pressed("Up"):
velocity.y -= 0.5
if Input.is_action_pressed ("Down"):
velocity.y += 0.5
if velocity == Vector2.ZERO:
speed = 0
animation_mode.travel("Idle")
else:
speed += acceleration #* delta
if speed > max_speed:
speed = max_speed
velocity = velocity.normalized() * speed
velocity = move_and_slide(velocity)
animation_tree.set("parameters/Walk/blend_position", velocity.normalized())
animation_tree.set("parameters/Idle/blend_position", velocity.normalized())
animation_mode.travel("Walk")
func MouseMovementLoop(delta):
if moving == false:
speed = 0
else:
speed += acceleration #* delta
if speed > max_speed:
speed = max_speed
movement = position.direction_to(destination) * speed
if position.distance_to(destination) > 10:
movement = move_and_slide(movement)
animation_tree.set("parameters/Walk/blend_position", movement.normalized())
animation_tree.set("parameters/Idle/blend_position", movement.normalized())
animation_mode.travel("Walk")
else:
moving = false
animation_mode.travel("Idle")
func MovementCombination(delta):
pass
As you can see i was trying two different ways of button related movement. Both are working but it seems that the acceleration in the "get_input" function doesnt work like the one in "ButtonMovementLoop". Also it seems that the different functions are getting irritated by each other after i injected seem into the "physics_process" function. As an example: If i activate one button movement function (no matter if it's "ButtonMovementLoop" or "get_input") together with the "MouseMovementLoop" the movement of the player seems to be a lot slower as if i only use one movement function. But as soon as i disable delta in the "speed += acceleration" part of the code the movement is normal fast as one would suspect it to be. If i inject only one movement related function into the physics_process instead there is no problem with delta and the movement is as intended. Another problem is that if i press a button at the same time as my player charakter is moving to the assinged destination by mouse click the charakter is getting a small boost in movement speed.
Another thing would be that i don't really now how to get the player charakter to only look to the destination (the direction) of the mouse pointer. I was trying it with "look_at(get_global_mouse_position()" but then only one sprite is getting rotated instead of that the correct player sprite is shown for one of the eight achses.
In generell i am very unexperienced in terms of programming (bet you thought that already - lol) so i am thankful for any help. In the godot documentation i haven't found something which would help getting my movement idea done.
Oh and please excuse my maybe a bit clumsy english. I am not a native speaker.
This is a picture of my node tree: