From 0823c295a3b03d98be356b239a3f74f05795d9c9 Mon Sep 17 00:00:00 2001 From: dusoleil Date: Sat, 23 Jul 2022 13:26:21 -0400 Subject: Smooth Rotation of Chickens --- Chicken.gd | 34 ++++++++++++++++++---------------- Chicken.tscn | 6 +++++- SmoothRotation.gd | 7 +++++++ 3 files changed, 30 insertions(+), 17 deletions(-) create mode 100644 SmoothRotation.gd diff --git a/Chicken.gd b/Chicken.gd index b9fb9a4..066d20f 100644 --- a/Chicken.gd +++ b/Chicken.gd @@ -2,6 +2,7 @@ extends KinematicBody export var rot_chance = 5.0 export var rot_range = PI / 4.0 +export var rot_speed = PI / 4.0 export var mov_chance = 5.0 export var mov_speed = 1.0 export var mov_length_min = 1.0 @@ -33,21 +34,22 @@ func _physics_process(delta): pass func state_idle(delta): - if roll_die(rot_chance): - var ang = (randf() - 0.5) * rot_range - rotate(Vector3.UP, ang) - if roll_die(mov_chance): - anim.state.travel("MOVING") - var local_forward = -transform.basis.z.normalized() - var dist = lerp(mov_length_min, mov_length_max, randf()) - var mov = local_forward * dist - mov_velocity = mov.normalized() * mov_speed - mov_target = translation + mov - mov_steps = (dist / mov_speed) / delta - elif roll_die(squack_chance): - anim.state.travel("SQUACK") - elif roll_die(peck_chance): - anim.state.travel("PECK") + if !$SmoothRotation.is_active(): + if roll_die(rot_chance): + var ang = (randf() - 0.5) * rot_range + $SmoothRotation.rotate_y(ang, rot_speed) + elif roll_die(mov_chance): + anim.state.travel("MOVING") + var local_forward = -transform.basis.z.normalized() + var dist = lerp(mov_length_min, mov_length_max, randf()) + var mov = local_forward * dist + mov_velocity = mov.normalized() * mov_speed + mov_target = transform.origin + mov + mov_steps = (dist / mov_speed) / delta + elif roll_die(squack_chance): + anim.state.travel("SQUACK") + elif roll_die(peck_chance): + anim.state.travel("PECK") var mov_velocity = Vector3.ZERO var mov_target = Vector3.ZERO @@ -61,7 +63,7 @@ func state_moving(_delta): var collision = get_slide_collision(index) if collision.collider.name == "Fence": mov_steps = 0.0 - look_at(translation + collision.normal ,Vector3.UP) + $SmoothRotation.rotate_y((-transform.basis.z.normalized()).signed_angle_to(collision.normal,Vector3.UP),rot_speed) mov_steps -= 1.0 if mov_steps < 1.0: mov_velocity *= mov_steps diff --git a/Chicken.tscn b/Chicken.tscn index 06d1bdb..835d872 100644 --- a/Chicken.tscn +++ b/Chicken.tscn @@ -1,9 +1,10 @@ -[gd_scene load_steps=49 format=2] +[gd_scene load_steps=50 format=2] [ext_resource path="res://chicken.tres" type="Material" id=1] [ext_resource path="res://beak.tres" type="Material" id=2] [ext_resource path="res://Chicken.gd" type="Script" id=3] [ext_resource path="res://ChickenAnimation.gd" type="Script" id=4] +[ext_resource path="res://SmoothRotation.gd" type="Script" id=5] [sub_resource type="SphereMesh" id=1] @@ -603,3 +604,6 @@ script = ExtResource( 4 ) [node name="CollisionShape" type="CollisionShape" parent="."] transform = Transform( 1, 0, 0, 0, 1.5, 0, 0, 0, 1.5, 0, 0.5, -0.5 ) shape = SubResource( 67 ) + +[node name="SmoothRotation" type="Tween" parent="."] +script = ExtResource( 5 ) diff --git a/SmoothRotation.gd b/SmoothRotation.gd new file mode 100644 index 0000000..f6df8c7 --- /dev/null +++ b/SmoothRotation.gd @@ -0,0 +1,7 @@ +extends Tween + +func rotate_y(ang, speed): + var parent = get_parent() + remove_all() + interpolate_property(parent, "rotation:y", parent.rotation.y, parent.rotation.y+ang,abs(ang/speed),Tween.TRANS_EXPO,Tween.EASE_OUT) + start() -- cgit v1.2.3