Hi,
Maybe you could try this:
extends KinematicBody
export(NodePath) onready var camera = get_node(camera) as Camera
var move_speed := 15.0
var mouse_pos := Vector3(0, 1, 0)
var ray_origin := Vector3()
var ray_end := Vector3()
var nav : Navigation
var space_state : PhysicsDirectSpaceState
var path := []
var path_index := 0
#This will determine how fast rotation to desired direction will happen
const angular_acceleration : float = 5.0
#Direction in which character is currently moving
var moveDir : Vector3 = Vector3.ZERO
func _ready() -> void:
if owner:
yield(owner, "ready") #waiting for ready function of the owner to be loaded
nav = owner.navigation
else:
print("no nav")
func _input(event: InputEvent) -> void:
if event.is_action_pressed("click_left"):
_mouse_world_pos()
func _physics_process(delta: float) -> void:
moveDir = Vector3.ZERO
if path_index < path.size():
moveDir = global_transform.origin.direction_to(path[path_index])
if global_transform.origin.distance_to(path[path_index]) < 1:
path_index += 1
else:
move_and_slide(moveDir * move_speed)
if(moveDir != Vector3.ZERO):
rotation.y = lerp_angle(rotation.y, atan2(moveDir.x, moveDir.z), delta * angular_acceleration)
func _mouse_world_pos() -> void:
#get current physics state
space_state = get_world().direct_space_state
#get current mous position in viewport
var mouse_position = get_viewport().get_mouse_position()
ray_origin = camera.project_ray_origin(mouse_position) #set ray origin
ray_end = ray_origin + camera.project_ray_normal(mouse_position) * 1000 #set ray end
var intersection = space_state.intersect_ray(ray_origin, ray_end, [self])
if intersection.collider:
path = nav.get_simple_path(global_transform.origin, intersection.position)
path_index = 0
var count = 0
for i in path:
var cube = MeshInstance.new()
cube.mesh = CubeMesh.new()
cube.translation = path[count]
owner.add_child(cube)
count += 1
That should rotate character to face direction it currently go assuming ground is flat.
*I haven't tested all this code myself, may not work right away.