I have now solved the problem so i'll just write my solution if anyone else should need the same system as I. It turned out the solution was not that complicated.
if Input.is_action_pressed("right"):
$AnimationPlayer.play("Run")
velocity += $Armature.transform.basis.x * SPEED
if Input.is_action_pressed("left"):
$AnimationPlayer.play("Run")
velocity += $Armature.transform.basis.x * -SPEED
if Input.is_action_pressed("forward"):
$AnimationPlayer.play("Run")
velocity += $Armature.transform.basis.z * -SPEED
if Input.is_action_pressed("backwards"):
$AnimationPlayer.play("Run")
velocity += $Armature.transform.basis.z * SPEED
I have added $Armature before transforming the basis. This seems locical looking back at it as i want the transform to happen local to the character object. (Armature in this context is a spatial within the Caharacter object containing the skeleton and mesh)
This gave me another problem, the mouse would move when the character did. This was solved by moving the logic of look_at_mouse_point()
to a script in the level instead of the characterscript. This is how the level script turned out.
extends StaticBody
var pos
var rayOrigin = Vector3()
var rayEnd = Vector3()
onready var camera = $Camera
func _physics_process(delta):
var space_state = get_world().direct_space_state
var mouse_position = get_viewport().get_mouse_position()
rayOrigin = camera.project_ray_origin(mouse_position)
rayEnd = rayOrigin + camera.project_ray_normal(mouse_position) *2000
var intersection = space_state.intersect_ray(rayOrigin, rayEnd)
if not intersection.empty():
pos = intersection.position
$Character/Armature.look_at(Vector3(pos.x, $Character.translation.y,
pos.z), Vector3.UP)