Smooth NPC turning

The Meldewesen now turns smoothly instead of abruptly. In addition,
while turning, its move speed decreases, causing it to mostly turn on
the spot.
This commit is contained in:
karl 2019-11-11 11:18:06 +01:00
parent 9fd3087fdc
commit 603e21b6fe
2 changed files with 28 additions and 6 deletions

View File

@ -2,7 +2,24 @@ extends KinematicBody
class_name NPC
var current_target = null
export(float) var turn_speed = 2.5
var current_target
var current_look_target
func _process(delta):
if current_look_target:
var diff_raw = (current_look_target - -transform.basis.z)
# Don't look up/down, only rotate on XZ plane
var diff = Vector3(diff_raw.x, 0.0, diff_raw.z)
# For the target, move the current forward direction towards the desired one
var target = -transform.basis.z + diff * turn_speed * delta
if diff.length() > 0.1:
look_at(transform.origin + target, Vector3.UP)
func set_position(position: Vector3):
@ -10,4 +27,12 @@ func set_position(position: Vector3):
func move_towards(direction_times_speed: Vector3):
move_and_slide(direction_times_speed)
# Decrease the speed depending on the difference between the look direction and the move direction
# This makes the NPC stop when turning, and only start moving once it has moved its gaze there
var speed_dir_factor = 1.0 - (-transform.basis.z.normalized() - direction_times_speed.normalized()).length() / 2.0
move_and_slide(direction_times_speed * speed_dir_factor)
func look_towards(direction: Vector3):
current_look_target = direction

View File

@ -51,10 +51,7 @@ func _process(delta):
body.move_towards(direction_normalized * speed)
# Look towards that goal as well
# Avoid look_at if we're already (almost) there, since that'd be an invalid look direction
# that can be identical to the up vector (which causes problems)
if direction.length() > 0.5:
body.look_at(_get_body_position() + direction_normalized, Vector3.UP)
body.look_towards(direction_normalized)
# Returns the point we should currently be moving towards