Also improved look at for NPCs.

main
Martin Felis 2024-11-15 11:35:37 +01:00
parent 8fdee7642c
commit e9db7b1095
2 changed files with 27 additions and 9 deletions

View File

@ -17,8 +17,10 @@ const JUMP_VELOCITY = 4.5
var tool_damage:bool = false
var tracking_node:Node3D = null
var look_direction:Vector3 = Vector3.BACK
var look_direction_damper:SpringDamper = SpringDamper.new(Vector3.ZERO, 2, 0.003, 0.003)
var _current_look_direction:Vector3 = Vector3.BACK
var _target_look_direction:Vector3 = Vector3.BACK
var _look_angle_damper:SpringDamper = SpringDamper.new(0, 2, 0.06, 0.003)
func _set_reacts_to_player(value:bool) -> bool:
if player_detection != null:
@ -36,18 +38,31 @@ func _ready() -> void:
animation_player.set_root("Geometry")
func _physics_process(delta: float) -> void:
var damped_look_direction = look_direction_damper.calc_clamped_speed(geometry.global_basis.z, look_direction, delta, 3.141)
_current_look_direction = basis.z
if tracking_node != null:
look_direction = (tracking_node.global_position - global_position).normalized()
_target_look_direction = (tracking_node.global_position - global_position).normalized()
look_direction.y = position.y
geometry.look_at(position - damped_look_direction, Vector3.UP)
update_look_direction(delta)
func _on_player_detection_body_entered(body: Node3D) -> void:
if body is Player:
tracking_node = body
func _on_player_detection_body_exited(body: Node3D) -> void:
look_direction = Vector3.BACK
if body == tracking_node:
tracking_node = null
_target_look_direction = Vector3.BACK
func update_look_direction(delta:float) -> void:
var current_look_angle:float = _current_look_direction.signed_angle_to(Vector3.BACK, Vector3.UP)
var target_look_angle:float = _target_look_direction.signed_angle_to(Vector3.BACK, Vector3.UP)
if target_look_angle - current_look_angle > PI:
current_look_angle = current_look_angle + PI * 2
elif current_look_angle - target_look_angle > PI:
current_look_angle = current_look_angle - PI * 2
var damped_look_angle:float = _look_angle_damper.calc(current_look_angle, target_look_angle, delta)
_current_look_direction = Vector3(sin(-damped_look_angle), 0, cos(-damped_look_angle))
self.basis = Basis.looking_at(-_current_look_direction)

View File

@ -35,7 +35,10 @@ func calc_clamped_speed(x, xt, h:float, s_max:float):
var x_new = calc(x, xt, h)
var vel_new = (x_new - x_old) / h
var speed_new = vel_new.length()
var speed_new = abs(vel_new)
if not vel_new is float:
speed_new = vel_new.length()
if speed_new > s_max:
vel_new = (vel_new / speed_new) * s_max
x = x_old + vel_new * h