Parcourir la source

More slope stuff

Marcel Zickler il y a 4 ans
Parent
commit
f64368b6fc
2 fichiers modifiés avec 84 ajouts et 8 suppressions
  1. 53 4
      Assets/Scenes/MainScene.unity
  2. 31 4
      Assets/Scripts/SlopeCollider.cs

+ 53 - 4
Assets/Scenes/MainScene.unity

@@ -197930,7 +197930,8 @@ GameObject:
   serializedVersion: 6
   m_Component:
   - component: {fileID: 814960441}
-  m_Layer: 9
+  - component: {fileID: 814960442}
+  m_Layer: 12
   m_Name: RearWheelContact
   m_TagString: Untagged
   m_Icon: {fileID: 0}
@@ -197945,12 +197946,25 @@ Transform:
   m_PrefabAsset: {fileID: 0}
   m_GameObject: {fileID: 814960440}
   m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
-  m_LocalPosition: {x: 0, y: -1.0378, z: -1}
+  m_LocalPosition: {x: 0, y: -1.0378, z: -0.98}
   m_LocalScale: {x: 1, y: 1, z: 1}
   m_Children: []
   m_Father: {fileID: 1842745497}
   m_RootOrder: 1
   m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
+--- !u!135 &814960442
+SphereCollider:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  m_GameObject: {fileID: 814960440}
+  m_Material: {fileID: 0}
+  m_IsTrigger: 0
+  m_Enabled: 1
+  serializedVersion: 2
+  m_Radius: 0.64
+  m_Center: {x: 0, y: 0.64, z: 0}
 --- !u!1001 &815116921
 PrefabInstance:
   m_ObjectHideFlags: 0
@@ -437371,7 +437385,8 @@ GameObject:
   serializedVersion: 6
   m_Component:
   - component: {fileID: 1862155314}
-  m_Layer: 9
+  - component: {fileID: 1862155315}
+  m_Layer: 12
   m_Name: FrontWheelContact
   m_TagString: Untagged
   m_Icon: {fileID: 0}
@@ -437386,12 +437401,25 @@ Transform:
   m_PrefabAsset: {fileID: 0}
   m_GameObject: {fileID: 1862155313}
   m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
-  m_LocalPosition: {x: 0, y: -1.0378, z: 1}
+  m_LocalPosition: {x: 0, y: -1.0378, z: 0.98}
   m_LocalScale: {x: 1, y: 1, z: 1}
   m_Children: []
   m_Father: {fileID: 1842745497}
   m_RootOrder: 0
   m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
+--- !u!135 &1862155315
+SphereCollider:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  m_GameObject: {fileID: 1862155313}
+  m_Material: {fileID: 0}
+  m_IsTrigger: 0
+  m_Enabled: 1
+  serializedVersion: 2
+  m_Radius: 0.64
+  m_Center: {x: 0, y: 0.64, z: 0}
 --- !u!1001 &1862393595
 PrefabInstance:
   m_ObjectHideFlags: 0
@@ -453770,6 +453798,11 @@ PrefabInstance:
       propertyPath: m_Enabled
       value: 1
       objectReference: {fileID: 0}
+    - target: {fileID: 2731252534111432329, guid: 3b07d6594636ab04d9c1fac04c1cde7e,
+        type: 3}
+      propertyPath: m_Enabled
+      value: 0
+      objectReference: {fileID: 0}
     - target: {fileID: 3450098086302837850, guid: 3b07d6594636ab04d9c1fac04c1cde7e,
         type: 3}
       propertyPath: centerOfMass
@@ -454642,6 +454675,21 @@ PrefabInstance:
       propertyPath: bike
       value: 
       objectReference: {fileID: 1689915699}
+    - target: {fileID: 7596273975278892917, guid: 3b07d6594636ab04d9c1fac04c1cde7e,
+        type: 3}
+      propertyPath: m_Enabled
+      value: 1
+      objectReference: {fileID: 0}
+    - target: {fileID: 7596273975278892917, guid: 3b07d6594636ab04d9c1fac04c1cde7e,
+        type: 3}
+      propertyPath: frontWheelCollider
+      value: 
+      objectReference: {fileID: 1862155315}
+    - target: {fileID: 7596273975278892917, guid: 3b07d6594636ab04d9c1fac04c1cde7e,
+        type: 3}
+      propertyPath: rearWheelCollider
+      value: 
+      objectReference: {fileID: 814960442}
     - target: {fileID: 8215765674596392599, guid: 3b07d6594636ab04d9c1fac04c1cde7e,
         type: 3}
       propertyPath: bicycleTransform
@@ -454670,6 +454718,7 @@ PrefabInstance:
     m_RemovedComponents:
     - {fileID: 6996791244483053872, guid: 3b07d6594636ab04d9c1fac04c1cde7e, type: 3}
     - {fileID: 4988978073311274400, guid: 3b07d6594636ab04d9c1fac04c1cde7e, type: 3}
+    - {fileID: 2731252534111432329, guid: 3b07d6594636ab04d9c1fac04c1cde7e, type: 3}
   m_SourcePrefab: {fileID: 100100000, guid: 3b07d6594636ab04d9c1fac04c1cde7e, type: 3}
 --- !u!1001 &1930089593
 PrefabInstance:

+ 31 - 4
Assets/Scripts/SlopeCollider.cs

@@ -33,16 +33,20 @@ internal struct SlopeHit
 public class SlopeCollider : MonoBehaviour
 {
     public int collisionLayer = 1;
-    public Transform rearWheelContactPoint;
-    public Transform frontWheelContactPoint;
+    public SphereCollider rearWheelCollider;
+    public SphereCollider frontWheelCollider;
     public Transform bike;
 
-    private const float THRESHOLD = 0f;
+    private const float THRESHOLD = 0.003f;
     private float distRwFw;
     private RotateDirection currentRotateDirection = RotateDirection.Unknown;
+    private Transform frontWheelContactPoint;
+    private Transform rearWheelContactPoint;
 
     private void Start()
     {
+        frontWheelContactPoint = frontWheelCollider.transform;
+        rearWheelContactPoint = rearWheelCollider.transform;
         distRwFw = Mathf.Abs(frontWheelContactPoint.localPosition.z - rearWheelContactPoint.localPosition.z);
     }
 
@@ -53,9 +57,32 @@ public class SlopeCollider : MonoBehaviour
         var fwHit = DrawRay(fwContact);
         var rwHit = DrawRay(rwContact);
 
+        if (!fwHit.HasValue || !rwHit.HasValue || rwHit.Value.even && fwHit.Value.even)
+        {
+            return;
+        }
+        
+        Debug.Log("----Slope Collider----");
+        Debug.Log($"\tfwHit: {fwHit}");
+        Debug.Log($"\trwHit: {rwHit}");
+
+        var fw = fwHit.Value;
+        var rw = rwHit.Value;
+        if (fw.distance >= THRESHOLD)
+        {
+            var angle = Mathf.Atan(fw.distance / distRwFw) * Mathf.Rad2Deg;
+            Debug.Log($"Rotating {angle} deg");
+            bike.RotateAround(rearWheelContactPoint.position, rearWheelContactPoint.right, angle);
+        }else if (rw.distance >= THRESHOLD)
+        {
+            var angle = Mathf.Atan(rw.distance / distRwFw) * Mathf.Rad2Deg;
+            bike.RotateAround(frontWheelContactPoint.position, frontWheelContactPoint.right, -angle);
+            Debug.Log($"Rotating {-angle} deg");
+        }
+
         //if ((fwHit?.even ?? true) && (rwHit?.even ?? true)) return;
 
-        var distFw = fwHit?.distance ?? 0f;
+        /*var distFw = fwHit?.distance ?? 0f;
         var distRw = rwHit?.distance ?? 0f;
         var distDif = distFw - distRw;