Update Villager.gd

This commit is contained in:
ncusimano 2023-08-19 16:41:21 -06:00
parent d5d7295602
commit 9208a53c71
1 changed files with 12 additions and 9 deletions

View File

@ -13,8 +13,8 @@ var task = "idle"
@export var walk_speed = 0.5
# Rate at which character corrects their direction after going off course (percentage).
@export var dir_correction_rate = 0.95
# Rate at which character corrects their direction after going off course (decimal percentage).
@export var spin_speed = 0.50
# Margin of accuracy to which the character will correct their direction when off course (radians).
@export var dir_accuracy = deg_to_rad(0.1)
@ -36,17 +36,17 @@ func _process(delta):
"idle":
task = rng.randf_range(0, 100)
# 3% chance to walk somewhere.
# Chance to walk somewhere.
if task <= walk_chance:
task = "walk"
target_location_xz = global_transform.origin * Vector3(1, 0, 1) + (direction_xz * 16)
# 6% chance to rotate.
# Chance to rotate.
elif task > walk_chance and task <= (walk_chance + spin_chance):
task = "spin"
var rotation_angle = rng.randf_range(-2*PI, 2*PI)
var rotation_vector = Vector3(cos(rotation_angle), 0, sin(rotation_angle))
target_direction_xz = direction_xz + rotation_vector
# 90% chance to idle.
# Chance to idle.
elif task > (spin_chance + walk_chance):
task = "idle"
@ -57,7 +57,7 @@ func _process(delta):
# Continue to correct direction to within a margin of dir_accuracy degrees.
if direction_xz.angle_to(target_direction_xz) >= dir_accuracy:
# Rotate towards destination at specified percentage rate.
rotate_y(direction_xz.angle_to(target_direction_xz) * dir_correction_rate)
rotate_y(direction_xz.angle_to(target_direction_xz) * spin_speed)
if location_xz.distance_to(target_location_xz) <= (Vector2(direction_xz.x, direction_xz.z) * walk_speed).length():
target_direction_xz = direction_xz
@ -65,10 +65,13 @@ func _process(delta):
task = "idle"
"spin":
if direction_xz.angle_to(target_direction_xz) >= dir_accuracy:
# Rotate towards destination at specified percentage rate.
rotate_y(direction_xz.angle_to(target_direction_xz) * spin_speed)
else:
task = "idle"
func _physics_process(delta):
# Add the gravity.
if not is_on_floor():