commit 9e773bf4923bba3d47474f22c5669d90272d0ddf
parent 51735f484a82781e9511412953baa9e33b927c1d
Author: FIGBERT <figbert@figbert.com>
Date: Thu, 31 Dec 2020 17:00:00 -0800
Add spin script to sevivon node
The spin script monitors the accelerometer to
detect a shake. When the user shakes the device,
the sevivon/dreidel tilts slightly on the X and Z
axes and significant torque is applied to the Y
axis.
Diffstat:
2 files changed, 19 insertions(+), 1 deletion(-)
diff --git a/scenes/main.tscn b/scenes/main.tscn
@@ -1,6 +1,7 @@
-[gd_scene load_steps=4 format=2]
+[gd_scene load_steps=5 format=2]
[ext_resource path="res://scenes/sevivon/default/sevivon.tscn" type="PackedScene" id=1]
+[ext_resource path="res://scripts/sevivon.gd" type="Script" id=2]
[sub_resource type="CubeMesh" id=1]
@@ -13,6 +14,7 @@ transform = Transform( 1, 0, 0, 0, 0.866025, 0.5, 0, -0.5, 0.866025, 0, 5, 6 )
[node name="Sevivon" parent="." instance=ExtResource( 1 )]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 2, 0 )
+script = ExtResource( 2 )
[node name="Floor" type="StaticBody" parent="."]
transform = Transform( 10, 0, 0, 0, 0.5, 0, 0, 0, 10, 0, -0.5, 0 )
diff --git a/scripts/sevivon.gd b/scripts/sevivon.gd
@@ -0,0 +1,16 @@
+extends RigidBody
+
+const ACCEL_THRESHOLD = 3
+const MIN_ANGLE = 0.0
+const MAX_ANGLE = 0.1
+const MIN_FORCE = 5
+const MAX_FORCE = 12
+var has_spun = false
+
+func _physics_process(delta):
+ var accel = Input.get_accelerometer()
+ if accel.length() > ACCEL_THRESHOLD:
+ randomize()
+ angular_velocity = Vector3(rand_range(MIN_ANGLE, MAX_ANGLE), 0, rand_range(MIN_ANGLE, MAX_ANGLE))
+ apply_torque_impulse(Vector3.UP * accel.length() * rand_range(MIN_FORCE, MAX_FORCE))
+ has_spun = true