Better player look at control.

main
Martin Felis 2024-11-15 11:05:01 +01:00
parent 2accf50133
commit 75fbb72da1
2 changed files with 32 additions and 15 deletions

View File

@ -24,9 +24,9 @@ var inventory:Inventory = Inventory.new()
var selected_tool_slot_index:int = 0
var current_tool:ItemResource = null
var _direction:Vector3 = Vector3.ZERO
var _look_direction:Vector3 = Vector3.BACK
var _look_direction_damper:SpringDamper = SpringDamper.new(Vector3.ZERO)
var _current_look_direction:Vector3 = Vector3.BACK
var _target_look_direction:Vector3 = Vector3.BACK
var _look_angle_damper:SpringDamper = SpringDamper.new(0, 4, 0.06, 0.003)
var is_input_blocked:bool = false
@ -40,10 +40,10 @@ func _handle_input() -> void:
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var input_dir = Input.get_vector("walk_left", "walk_right", "walk_forward", "walk_back")
_direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
if _direction:
velocity.x = _direction.x * SPEED
velocity.z = _direction.z * SPEED
var direction = Vector3(input_dir.x, 0, input_dir.y).normalized()
if direction:
velocity.x = direction.x * SPEED
velocity.z = direction.z * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
velocity.z = move_toward(velocity.z, 0, SPEED)
@ -62,19 +62,35 @@ func _physics_process(delta):
move_and_slide()
var ground_velocity:Vector2 = Vector2(velocity.x, velocity.z)
var is_moving:bool = ground_velocity.length_squared() > 0.1 * 0.1
if is_moving:
_look_direction = Vector3(ground_velocity.x, 0, ground_velocity.y).normalized()
elif _direction.length_squared() > 0.1 * 0.1:
_look_direction = _direction
_target_look_direction = Vector3(velocity.x, 0, velocity.z).normalized()
var damped_look_direction = _look_direction_damper.calc(global_basis.z, _look_direction, delta)
_current_look_direction = basis.z
#_target_look_direction = _current_look_direction
update_look_direction(delta)
animation_tree.set("parameters/conditions/running", is_moving)
animation_tree.set("parameters/conditions/idle", not is_moving)
geometry.look_at(position - damped_look_direction, Vector3.UP)
func init_look_target_direction() -> void:
_target_look_direction = basis.z
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)
func set_tool_slot_index(value:int) -> void:
if inventory == null:
@ -138,7 +154,7 @@ func _unhandled_input(_event: InputEvent) -> void:
if Input.is_action_just_pressed("ui_accept"):
var actionables = actionable_detector.get_overlapping_areas()
if actionables.size() > 0:
_look_direction = (actionables[0].global_position - position).normalized()
_target_look_direction = (actionables[0].global_position - position).normalized()
actionables[0].action()
get_viewport().set_input_as_handled()

View File

@ -107,6 +107,7 @@ func load_game():
return
player.global_transform = save_data.player_transform
player.init_look_target_direction()
# Quest state
for quest_state:Node in get_tree().get_nodes_in_group("quest_state"):