add unit gradual turning
This commit is contained in:
@@ -1,7 +1,8 @@
|
|||||||
extends CharacterBody3D
|
extends CharacterBody3D
|
||||||
class_name TestUnit
|
class_name TestUnit
|
||||||
|
|
||||||
const SPEED: float = 3
|
const MOVE_SPEED: float = 3
|
||||||
|
const TURN_SPEED: float = 10
|
||||||
|
|
||||||
var selected: bool = false
|
var selected: bool = false
|
||||||
var ground_plane: Plane = Plane(Vector3.UP, 0)
|
var ground_plane: Plane = Plane(Vector3.UP, 0)
|
||||||
@@ -14,12 +15,12 @@ var ground_plane: Plane = Plane(Vector3.UP, 0)
|
|||||||
|
|
||||||
func _ready() -> void:
|
func _ready() -> void:
|
||||||
set_selected(false)
|
set_selected(false)
|
||||||
nav_agent.max_speed = SPEED
|
nav_agent.max_speed = MOVE_SPEED
|
||||||
nav_agent.velocity_computed.connect(_on_nav_agent_velocity_computed)
|
nav_agent.velocity_computed.connect(_on_nav_agent_velocity_computed)
|
||||||
|
|
||||||
|
|
||||||
func _process(_delta: float) -> void:
|
func _process(delta: float) -> void:
|
||||||
_animate()
|
_animate(delta)
|
||||||
|
|
||||||
|
|
||||||
func _physics_process(_delta: float) -> void:
|
func _physics_process(_delta: float) -> void:
|
||||||
@@ -64,13 +65,16 @@ func _navigate() -> void:
|
|||||||
var next_pos := nav_agent.get_next_path_position()
|
var next_pos := nav_agent.get_next_path_position()
|
||||||
|
|
||||||
var direction := global_position.direction_to(next_pos)
|
var direction := global_position.direction_to(next_pos)
|
||||||
var new_velocity := direction * SPEED
|
var new_velocity := direction * MOVE_SPEED
|
||||||
nav_agent.set_velocity(new_velocity)
|
nav_agent.set_velocity(new_velocity)
|
||||||
|
|
||||||
|
|
||||||
func _animate() -> void:
|
func _animate(delta: float) -> void:
|
||||||
if velocity.length() > 0.1:
|
if velocity.length() > 0.1:
|
||||||
look_at(global_position + velocity, Vector3.UP, true)
|
var velocity_normalized := velocity.normalized()
|
||||||
|
var angle := atan2(-velocity_normalized.x, -velocity_normalized.z) + PI
|
||||||
|
global_rotation.y = rotate_toward(global_rotation.y, angle, TURN_SPEED * delta)
|
||||||
|
# look_at(global_position + velocity, Vector3.UP, true)
|
||||||
animation_player.play('walk')
|
animation_player.play('walk')
|
||||||
else:
|
else:
|
||||||
animation_player.play('idle')
|
animation_player.play('idle')
|
||||||
|
|||||||
Reference in New Issue
Block a user