Browse Source

Almost good bike tilting

Marcel Zickler 3 years ago
parent
commit
97dfb55e3d

+ 35 - 4
Assets/Scenes/MainScene.unity

@@ -68937,6 +68937,36 @@ Transform:
   m_Father: {fileID: 1831341202}
   m_RootOrder: 256
   m_LocalEulerAnglesHint: {x: 0, y: 90, z: 0}
+--- !u!1 &457671767
+GameObject:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  serializedVersion: 6
+  m_Component:
+  - component: {fileID: 457671768}
+  m_Layer: 9
+  m_Name: rotatePoint
+  m_TagString: Untagged
+  m_Icon: {fileID: 0}
+  m_NavMeshLayer: 0
+  m_StaticEditorFlags: 0
+  m_IsActive: 1
+--- !u!4 &457671768
+Transform:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  m_GameObject: {fileID: 457671767}
+  m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
+  m_LocalPosition: {x: 0, y: -1.034, z: -0.98041}
+  m_LocalScale: {x: 1, y: 1, z: 1}
+  m_Children: []
+  m_Father: {fileID: 1689915699}
+  m_RootOrder: 6
+  m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
 --- !u!1001 &458169113
 PrefabInstance:
   m_ObjectHideFlags: 0
@@ -241628,13 +241658,14 @@ MonoBehaviour:
   m_PrefabInstance: {fileID: 0}
   m_PrefabAsset: {fileID: 0}
   m_GameObject: {fileID: 1689915694}
-  m_Enabled: 0
+  m_Enabled: 1
   m_EditorHideFlags: 0
   m_Script: {fileID: 11500000, guid: 4eb59bc0bf1db89469a465ebea232d16, type: 3}
   m_Name: 
   m_EditorClassIdentifier: 
   frontWheel: {fileID: 1788657551}
   rearWheel: {fileID: 1877071838}
+  rotatePoint: {fileID: 457671768}
   timeDifferenceTolerance: 0.1
   dif: {x: 0, y: 0, z: 0}
 --- !u!1001 &1690811636
@@ -291425,17 +291456,17 @@ PrefabInstance:
     - target: {fileID: 1121772855208284805, guid: a8075fecadfce7c4abe277fb835ca5c5,
         type: 3}
       propertyPath: m_LocalRotation.x
-      value: -0
+      value: 0
       objectReference: {fileID: 0}
     - target: {fileID: 1121772855208284805, guid: a8075fecadfce7c4abe277fb835ca5c5,
         type: 3}
       propertyPath: m_LocalRotation.y
-      value: -0
+      value: 0
       objectReference: {fileID: 0}
     - target: {fileID: 1121772855208284805, guid: a8075fecadfce7c4abe277fb835ca5c5,
         type: 3}
       propertyPath: m_LocalRotation.z
-      value: -0
+      value: 0
       objectReference: {fileID: 0}
     - target: {fileID: 1121772855208284805, guid: a8075fecadfce7c4abe277fb835ca5c5,
         type: 3}

+ 10 - 4
Assets/Scripts/CustomWheelCollider.cs

@@ -1,4 +1,5 @@
 using System;
+using System.Linq;
 using UnityEngine;
 
 public struct HitInfo
@@ -26,6 +27,7 @@ public class CustomWheelCollider : MonoBehaviour
     private Vector3[] rayDirections;
 
     public HitInfo? CurrentHit { private set; get; } = null;
+    public int CurrentHitNumber { private set; get; }
 
     private void Start()
     {
@@ -35,10 +37,14 @@ public class CustomWheelCollider : MonoBehaviour
 
     private void FixedUpdate()
     {
+        var results = new HitInfo?[numberOfRays];
         for (var i = 0; i < numberOfRays; i++)
         {
-            CastRay(i);
+            results[i] = CastRay(i);
         }
+
+        CurrentHit = results.FirstOrDefault(r => r != null);
+        CurrentHitNumber = results.Count(r => r != null);
     }
 
     public Vector3[] CalculateRayDirections()
@@ -73,7 +79,7 @@ public class CustomWheelCollider : MonoBehaviour
     }*/
 
 
-    private void CastRay(int index)
+    private HitInfo? CastRay(int index)
     {
         var direction = t.TransformDirection(rayDirections[index]);
         var layerMask = 1 << collisionLayer;
@@ -86,12 +92,12 @@ public class CustomWheelCollider : MonoBehaviour
         {
             Debug.DrawRay(t.position, direction * hit.distance, Color.yellow);
             //Debug.Log("Did Hit: ");
-            CurrentHit = new HitInfo(Time.fixedTime, hit);
+            return new HitInfo(Time.fixedTime, hit);
         }
         else
         {
             Debug.DrawRay(t.position, direction * radius, Color.white);
-            //Debug.Log("Did not Hit");
+            return null;
         }
     }
 }

+ 41 - 4
Assets/Scripts/CustomWheelColliderSlopeAdjustment.cs

@@ -7,12 +7,21 @@ public class CustomWheelColliderSlopeAdjustment : MonoBehaviour
 {
     public CustomWheelCollider frontWheel;
     public CustomWheelCollider rearWheel;
-    public float timeDifferenceTolerance = 0.1f;
+    public Transform rotatePoint;
+    public float timeDifferenceTolerance = 0.0001f;
 
     public Vector3 dif; //TODO: remove
 
     private Transform t;
-    
+
+    private bool isHitting = false;
+    private float angle = 0f;
+
+    private void OnGUI()
+    {
+        GUI.TextField(new Rect(Screen.width - 200, 10, 190, 80), $"Hitting? {isHitting}\nAngle: {angle:n2}\nrw: {rearWheel.CurrentHit?.Hit.point}\nfw: {frontWheel.CurrentHit?.Hit.point}");
+    }
+
     private void Start()
     {
         t = transform;
@@ -20,6 +29,22 @@ public class CustomWheelColliderSlopeAdjustment : MonoBehaviour
 
     private void FixedUpdate()
     {
+        isHitting = false;
+
+        if (frontWheel.CurrentHitNumber > 1)
+        {
+            angle = -.5f;
+            Rotate();
+            return;
+        }
+        if (rearWheel.CurrentHitNumber > 1)
+        {
+            angle = .5f;
+            Rotate();
+            return;
+        }
+        
+        //TODO: this is probably called almost never
         if (frontWheel.CurrentHit == null || rearWheel.CurrentHit == null) return;
             
         var fwHit = frontWheel.CurrentHit.Value;
@@ -33,8 +58,20 @@ public class CustomWheelColliderSlopeAdjustment : MonoBehaviour
 
     private void UpdateTransformRotation(HitInfo fwHit, HitInfo rwHit)
     {
+        isHitting = true;
         dif = t.TransformPoint(fwHit.Hit.point) - t.TransformPoint(rwHit.Hit.point);
-        var angle = -Mathf.Atan(dif.y / dif.z) * Mathf.Rad2Deg; //TODO: is it that easy?
-        t.localRotation = Quaternion.Euler(t.localRotation.eulerAngles + Vector3.right * angle);
+        angle = -Mathf.Atan(dif.y / dif.z) * Mathf.Rad2Deg; //TODO: is it that easy?
+        
+        Debug.DrawLine(rwHit.Hit.point, rwHit.Hit.point + t.right, Color.red);
+        
+        Rotate();
+        //t.RotateAround(rotatePoint.position, t.right, angle) ;
+        //t.localRotation = Quaternion.Euler(t.localRotation.eulerAngles + Vector3.right * angle);
+    }
+
+    private void Rotate()
+    {
+        t.RotateAround(rotatePoint.position, t.right, angle) ;
+        angle = 0f;
     }
 }

+ 6 - 6
VR Cycling.sln

@@ -9,10 +9,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SteamVR_Actions", "SteamVR_
 EndProject
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SteamVR_Editor", "SteamVR_Editor.csproj", "{D8C7B615-CB75-29C4-6CE4-D38FB9BA7D1D}"
 EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SteamVR_Input_Editor", "SteamVR_Input_Editor.csproj", "{1140961C-B987-84EB-8395-EA3F31446727}"
-EndProject
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Assembly-CSharp-Editor", "Assembly-CSharp-Editor.csproj", "{6EA546DC-64DE-308D-C107-3A365DA0CE9C}"
 EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SteamVR_Input_Editor", "SteamVR_Input_Editor.csproj", "{1140961C-B987-84EB-8395-EA3F31446727}"
+EndProject
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SteamVR_Windows_EditorHelper", "SteamVR_Windows_EditorHelper.csproj", "{CDC4B8D5-119D-91FE-D16D-283F6632AFF5}"
 EndProject
 Global
@@ -37,14 +37,14 @@ Global
 		{D8C7B615-CB75-29C4-6CE4-D38FB9BA7D1D}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{D8C7B615-CB75-29C4-6CE4-D38FB9BA7D1D}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{D8C7B615-CB75-29C4-6CE4-D38FB9BA7D1D}.Release|Any CPU.Build.0 = Release|Any CPU
-		{1140961C-B987-84EB-8395-EA3F31446727}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
-		{1140961C-B987-84EB-8395-EA3F31446727}.Debug|Any CPU.Build.0 = Debug|Any CPU
-		{1140961C-B987-84EB-8395-EA3F31446727}.Release|Any CPU.ActiveCfg = Release|Any CPU
-		{1140961C-B987-84EB-8395-EA3F31446727}.Release|Any CPU.Build.0 = Release|Any CPU
 		{6EA546DC-64DE-308D-C107-3A365DA0CE9C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
 		{6EA546DC-64DE-308D-C107-3A365DA0CE9C}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{6EA546DC-64DE-308D-C107-3A365DA0CE9C}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{6EA546DC-64DE-308D-C107-3A365DA0CE9C}.Release|Any CPU.Build.0 = Release|Any CPU
+		{1140961C-B987-84EB-8395-EA3F31446727}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{1140961C-B987-84EB-8395-EA3F31446727}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{1140961C-B987-84EB-8395-EA3F31446727}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{1140961C-B987-84EB-8395-EA3F31446727}.Release|Any CPU.Build.0 = Release|Any CPU
 		{CDC4B8D5-119D-91FE-D16D-283F6632AFF5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
 		{CDC4B8D5-119D-91FE-D16D-283F6632AFF5}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{CDC4B8D5-119D-91FE-D16D-283F6632AFF5}.Release|Any CPU.ActiveCfg = Release|Any CPU

BIN
obj/Debug/Assembly-CSharp.csprojAssemblyReference.cache