Selaa lähdekoodia

Fixed wheel and fork animation

Marcel Zickler 3 vuotta sitten
vanhempi
commit
1ee078ec58

+ 9 - 6
Assets/Editor/FrontWheelTrackerEditor.cs

@@ -14,8 +14,11 @@ public class FrontWheelTrackerEditor : Editor
     private void OnEnable()
     {
         fwt = (FrontWheelTracker) target;
-        radiusProperty = serializedObject.FindProperty("trackerDistanceToWheelCenterPoint");
-        fwt.Init();
+        if (fwt.isActiveAndEnabled)
+        {
+            radiusProperty = serializedObject.FindProperty("trackerDistanceToWheelCenterPoint");
+            fwt.Init();
+        }
     }
 
     public override void OnInspectorGUI()
@@ -26,20 +29,20 @@ public class FrontWheelTrackerEditor : Editor
         fwt.bicycleTransform = (Transform) EditorGUILayout.ObjectField("Bicycle Transform", fwt.bicycleTransform,
             typeof(Transform), false);
 
-        //if (EditorApplication.isPlaying)
-        //{
+        if (fwt.isActiveAndEnabled)
+        {
             EditorGUILayout.Space();
             EditorGUILayout.LabelField("Attach Vive Tracker to Wheel and press Button to calibrate");
             if (GUILayout.Button("Calibrate"))fwt.Calibrate();
             EditorGUILayout.Space();
-        //}
+        }
 
         fwt.trackerDistanceToWheelCenterPoint = EditorGUILayout.FloatField(
             new GUIContent("Tracker Distance to Hub",
                 "Distance of Vive Tracker to The Center of the wheel hub on the x,z plane of the tracker"),
             fwt.trackerDistanceToWheelCenterPoint);
 
-        //if (!EditorApplication.isPlaying) return;
+        if (!fwt.isActiveAndEnabled) return;
 
         EditorGUILayout.LabelField("Rotate Wheel to 90 degrees to calibrate radius");
         if (GUILayout.Button("Calibrate Radius")) CalibrateRadius();

+ 135 - 3
Assets/Scenes/MainScene.unity

@@ -38,7 +38,7 @@ RenderSettings:
   m_ReflectionIntensity: 1
   m_CustomReflection: {fileID: 0}
   m_Sun: {fileID: 0}
-  m_IndirectSpecularColor: {r: 0.28462106, g: 0.37120986, b: 0.4988058, a: 1}
+  m_IndirectSpecularColor: {r: 0.2846213, g: 0.37121028, b: 0.49880686, a: 1}
   m_UseRadianceAmbientProbe: 0
 --- !u!157 &3
 LightmapSettings:
@@ -226887,6 +226887,12 @@ PrefabInstance:
       objectReference: {fileID: 0}
     m_RemovedComponents: []
   m_SourcePrefab: {fileID: 100100000, guid: a8075fecadfce7c4abe277fb835ca5c5, type: 3}
+--- !u!1 &1788657547 stripped
+GameObject:
+  m_CorrespondingSourceObject: {fileID: 4988978072139883315, guid: 3b07d6594636ab04d9c1fac04c1cde7e,
+    type: 3}
+  m_PrefabInstance: {fileID: 1929752190}
+  m_PrefabAsset: {fileID: 0}
 --- !u!1001 &1788701980
 PrefabInstance:
   m_ObjectHideFlags: 0
@@ -239612,6 +239618,12 @@ Transform:
     type: 3}
   m_PrefabInstance: {fileID: 1002812097}
   m_PrefabAsset: {fileID: 0}
+--- !u!1 &1877071834 stripped
+GameObject:
+  m_CorrespondingSourceObject: {fileID: 4988978072291817852, guid: 3b07d6594636ab04d9c1fac04c1cde7e,
+    type: 3}
+  m_PrefabInstance: {fileID: 1929752190}
+  m_PrefabAsset: {fileID: 0}
 --- !u!1001 &1877335240
 PrefabInstance:
   m_ObjectHideFlags: 0
@@ -245948,6 +245960,11 @@ PrefabInstance:
       propertyPath: maxSpeed
       value: 11.111
       objectReference: {fileID: 0}
+    - target: {fileID: 4988978071383829745, guid: 3b07d6594636ab04d9c1fac04c1cde7e,
+        type: 3}
+      propertyPath: m_IsActive
+      value: 0
+      objectReference: {fileID: 0}
     - target: {fileID: 4988978071383829747, guid: 3b07d6594636ab04d9c1fac04c1cde7e,
         type: 3}
       propertyPath: index
@@ -245961,7 +245978,7 @@ PrefabInstance:
     - target: {fileID: 4988978071383829751, guid: 3b07d6594636ab04d9c1fac04c1cde7e,
         type: 3}
       propertyPath: trackerDistanceToWheelCenterPoint
-      value: 0.283
+      value: -0
       objectReference: {fileID: 0}
     - target: {fileID: 4988978072012181939, guid: 3b07d6594636ab04d9c1fac04c1cde7e,
         type: 3}
@@ -246036,7 +246053,7 @@ PrefabInstance:
     - target: {fileID: 4988978072042319893, guid: 3b07d6594636ab04d9c1fac04c1cde7e,
         type: 3}
       propertyPath: m_Enabled
-      value: 1
+      value: 0
       objectReference: {fileID: 0}
     - target: {fileID: 4988978072042319893, guid: 3b07d6594636ab04d9c1fac04c1cde7e,
         type: 3}
@@ -246058,11 +246075,96 @@ PrefabInstance:
       propertyPath: m_Enabled
       value: 1
       objectReference: {fileID: 0}
+    - target: {fileID: 4988978072042319978, guid: 3b07d6594636ab04d9c1fac04c1cde7e,
+        type: 3}
+      propertyPath: m_Enabled
+      value: 1
+      objectReference: {fileID: 0}
     - target: {fileID: 4988978072042319979, guid: 3b07d6594636ab04d9c1fac04c1cde7e,
         type: 3}
       propertyPath: m_Enabled
       value: 0
       objectReference: {fileID: 0}
+    - target: {fileID: 4988978072042319980, guid: 3b07d6594636ab04d9c1fac04c1cde7e,
+        type: 3}
+      propertyPath: m_Enabled
+      value: 1
+      objectReference: {fileID: 0}
+    - target: {fileID: 4988978072042319980, guid: 3b07d6594636ab04d9c1fac04c1cde7e,
+        type: 3}
+      propertyPath: speedMultiplier
+      value: 4
+      objectReference: {fileID: 0}
+    - target: {fileID: 4988978072042319980, guid: 3b07d6594636ab04d9c1fac04c1cde7e,
+        type: 3}
+      propertyPath: steerMultiplier
+      value: 35
+      objectReference: {fileID: 0}
+    - target: {fileID: 4988978072042319980, guid: 3b07d6594636ab04d9c1fac04c1cde7e,
+        type: 3}
+      propertyPath: useLean
+      value: 0
+      objectReference: {fileID: 0}
+    - target: {fileID: 4988978072139883316, guid: 3b07d6594636ab04d9c1fac04c1cde7e,
+        type: 3}
+      propertyPath: m_LocalRotation.x
+      value: 1
+      objectReference: {fileID: 0}
+    - target: {fileID: 4988978072139883316, guid: 3b07d6594636ab04d9c1fac04c1cde7e,
+        type: 3}
+      propertyPath: m_LocalRotation.w
+      value: 0
+      objectReference: {fileID: 0}
+    - target: {fileID: 4988978072139883316, guid: 3b07d6594636ab04d9c1fac04c1cde7e,
+        type: 3}
+      propertyPath: m_LocalEulerAnglesHint.x
+      value: 0
+      objectReference: {fileID: 0}
+    - target: {fileID: 4988978072139883316, guid: 3b07d6594636ab04d9c1fac04c1cde7e,
+        type: 3}
+      propertyPath: m_LocalRotation.y
+      value: 0
+      objectReference: {fileID: 0}
+    - target: {fileID: 4988978072139883316, guid: 3b07d6594636ab04d9c1fac04c1cde7e,
+        type: 3}
+      propertyPath: m_LocalRotation.z
+      value: 0
+      objectReference: {fileID: 0}
+    - target: {fileID: 4988978072139883316, guid: 3b07d6594636ab04d9c1fac04c1cde7e,
+        type: 3}
+      propertyPath: m_LocalEulerAnglesHint.y
+      value: 180
+      objectReference: {fileID: 0}
+    - target: {fileID: 4988978072139883316, guid: 3b07d6594636ab04d9c1fac04c1cde7e,
+        type: 3}
+      propertyPath: m_LocalEulerAnglesHint.z
+      value: 180
+      objectReference: {fileID: 0}
+    - target: {fileID: 4988978072291817853, guid: 3b07d6594636ab04d9c1fac04c1cde7e,
+        type: 3}
+      propertyPath: m_LocalRotation.x
+      value: 0
+      objectReference: {fileID: 0}
+    - target: {fileID: 4988978072291817853, guid: 3b07d6594636ab04d9c1fac04c1cde7e,
+        type: 3}
+      propertyPath: m_LocalRotation.y
+      value: 0
+      objectReference: {fileID: 0}
+    - target: {fileID: 4988978072291817853, guid: 3b07d6594636ab04d9c1fac04c1cde7e,
+        type: 3}
+      propertyPath: m_LocalRotation.z
+      value: 0
+      objectReference: {fileID: 0}
+    - target: {fileID: 4988978072291817853, guid: 3b07d6594636ab04d9c1fac04c1cde7e,
+        type: 3}
+      propertyPath: m_LocalRotation.w
+      value: 1
+      objectReference: {fileID: 0}
+    - target: {fileID: 4988978072291817853, guid: 3b07d6594636ab04d9c1fac04c1cde7e,
+        type: 3}
+      propertyPath: m_LocalEulerAnglesHint.x
+      value: 0
+      objectReference: {fileID: 0}
     - target: {fileID: 4988978072311855431, guid: 3b07d6594636ab04d9c1fac04c1cde7e,
         type: 3}
       propertyPath: m_VolumeLayerMask.m_Bits
@@ -246154,6 +246256,36 @@ PrefabInstance:
       propertyPath: m_LocalEulerAnglesHint.y
       value: 0
       objectReference: {fileID: 0}
+    - target: {fileID: 4988978073311274404, guid: 3b07d6594636ab04d9c1fac04c1cde7e,
+        type: 3}
+      propertyPath: frontWheel.wheel
+      value: 
+      objectReference: {fileID: 1788657547}
+    - target: {fileID: 4988978073311274404, guid: 3b07d6594636ab04d9c1fac04c1cde7e,
+        type: 3}
+      propertyPath: rearWheel.wheel
+      value: 
+      objectReference: {fileID: 1877071834}
+    - target: {fileID: 4988978073311274404, guid: 3b07d6594636ab04d9c1fac04c1cde7e,
+        type: 3}
+      propertyPath: rearWheel.rotationDirection
+      value: 1
+      objectReference: {fileID: 0}
+    - target: {fileID: 4988978073311274404, guid: 3b07d6594636ab04d9c1fac04c1cde7e,
+        type: 3}
+      propertyPath: frontWheel.rotationDirection
+      value: 1
+      objectReference: {fileID: 0}
+    - target: {fileID: 4988978073311274404, guid: 3b07d6594636ab04d9c1fac04c1cde7e,
+        type: 3}
+      propertyPath: rearWheel.rotationAxis
+      value: 0
+      objectReference: {fileID: 0}
+    - target: {fileID: 4988978073311274404, guid: 3b07d6594636ab04d9c1fac04c1cde7e,
+        type: 3}
+      propertyPath: frontWheel.rotationAxis
+      value: 0
+      objectReference: {fileID: 0}
     - target: {fileID: 4988978073343560568, guid: 3b07d6594636ab04d9c1fac04c1cde7e,
         type: 3}
       propertyPath: m_LocalPosition.y

+ 59 - 8
Assets/Scripts/Animation/BikeAnimation.cs

@@ -1,4 +1,5 @@
-using System.Collections.Generic;
+using System;
+using System.Collections.Generic;
 using Controller;
 using Controller.Bicycle;
 using Sensors;
@@ -6,15 +7,40 @@ using UnityEngine;
 
 namespace Animation
 {
+    #region Helpers
+
+    public enum WheelRotationAxis
+    {
+        X,
+        Y,
+        Z
+    }
+
+    public enum WheelRotationDirection
+    {
+        Forward,
+        Backward
+    }
+
+    [Serializable]
+    public struct WheelAnimationConfig
+    {
+        public GameObject wheel;
+        public WheelRotationAxis rotationAxis;
+        public WheelRotationDirection rotationDirection;
+    }
+
+    #endregion
+
     [RequireComponent(typeof(IBicycleController))]
     public class BikeAnimation : MonoBehaviour
     {
         #region Variables
 
         [Header("Wheels - rotating around center")]
-        public GameObject rearWheel;
+        public WheelAnimationConfig rearWheel;
 
-        public GameObject frontWheel;
+        public WheelAnimationConfig frontWheel;
 
         [Header("Other with defined rotation axis")]
         public Rotatable crankSet;
@@ -56,11 +82,36 @@ namespace Animation
         {
             var speed = bicycleController.CurrentSpeed;
             var rps = speed / wheelCircumference;
-            var angle = rps * Time.deltaTime;
-        
-            //TODO: only works for this specific model
-            rearWheel.transform.RotateAround(rearWheel.transform.position, -rearWheel.transform.right, angle);
-            frontWheel.transform.RotateAround(frontWheel.transform.position, -frontWheel.transform.right, angle);
+            var angle = rps * Time.deltaTime * 360f;
+
+            RotateSingleWheel(frontWheel, angle);
+            RotateSingleWheel(rearWheel, angle);
+        }
+
+        private void RotateSingleWheel(WheelAnimationConfig wheel, float angle)
+        {
+            var t = wheel.wheel.transform;
+            var oldPos = t.localPosition;
+            var axis = wheel.rotationAxis;
+            var backwards = wheel.rotationDirection == WheelRotationDirection.Backward;
+            var directedAngle = backwards ? -angle : angle;
+
+            Vector3 rotateAxis;
+            if (axis == WheelRotationAxis.X)
+            {
+                rotateAxis = t.right;
+            }
+            else if (axis == WheelRotationAxis.Y)
+            {
+                rotateAxis = t.up;
+            }
+            else
+            {
+                rotateAxis = t.forward;
+            }
+
+            t.RotateAround(t.position, rotateAxis, directedAngle);
+            t.localPosition = oldPos;
         }
 
         private void RotateAroundDefinedAxis(Rotatable rotatable, float angle, bool resetBeforeRotate = true)

+ 1 - 0
Assets/Scripts/Display/ViveTrackerDebugDisplay.cs

@@ -20,6 +20,7 @@ namespace Display
         // Update is called once per frame
         private void Update()
         {
+            if (!tracker.isActiveAndEnabled) return;
             posText.text = $"Pos: {tracker.Position.ToString()}";
             rotText.text = $"Rot: {tracker.Rotation.ToString()}";
         }