123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- using System;
- using UnityEngine;
- public enum RotateDirection
- {
- Unknown,
- None,
- Forward,
- Backward
- }
- internal struct SlopeHit
- {
- internal float distance;
- internal bool even;
- internal float angle;
- internal Vector3 hitPoint;
- internal SlopeHit(float distance, bool even, float angle, Vector3 hitPoint)
- {
- this.distance = distance;
- this.even = even;
- this.angle = angle;
- this.hitPoint = hitPoint;
- }
- public override string ToString()
- {
- return $"SlopeHit(distance = {distance}, even = {even}, angle = {angle})";
- }
- }
- public class SlopeCollider : MonoBehaviour
- {
- public int collisionLayer = 1;
- public Transform rearWheelContactPoint;
- public Transform frontWheelContactPoint;
- public Transform bike;
- private const float THRESHOLD = 0f;
- private float distRwFw;
- private RotateDirection currentRotateDirection = RotateDirection.Unknown;
- private void Start()
- {
- distRwFw = Mathf.Abs(frontWheelContactPoint.localPosition.z - rearWheelContactPoint.localPosition.z);
- }
- private void FixedUpdate()
- {
- var fwContact = frontWheelContactPoint.position;
- var rwContact = rearWheelContactPoint.position;
- var fwHit = DrawRay(fwContact);
- var rwHit = DrawRay(rwContact);
-
- var distFw = fwHit?.distance ?? 0f;
- var distRw = rwHit?.distance ?? 0f;
- var distDif = distFw - distRw;
-
- if (fwHit.HasValue && rwHit.HasValue)
- {
- var fw = fwHit.Value;
- var rw = rwHit.Value;
- if (!fw.even && rw.even)
- {
- if (fw.hitPoint.y > rw.hitPoint.y && currentRotateDirection != RotateDirection.Forward)
- {
- currentRotateDirection = RotateDirection.Backward;
- }else if (currentRotateDirection != RotateDirection.Backward)
- {
- currentRotateDirection = RotateDirection.Forward;
- }
- }else if (fw.even && !rw.even)
- {
- if (fw.hitPoint.y > rw.hitPoint.y && currentRotateDirection != RotateDirection.Backward)
- {
- currentRotateDirection = RotateDirection.Forward;
- }
- else if(currentRotateDirection != RotateDirection.Forward)
- {
- currentRotateDirection = RotateDirection.Backward;
- }
-
- }
- else
- {
- currentRotateDirection = RotateDirection.None;
- }
- }
- else
- {
- if (distDif < -THRESHOLD && currentRotateDirection != RotateDirection.Forward)
- {
- currentRotateDirection = RotateDirection.Backward;
- }else if (distDif > THRESHOLD && currentRotateDirection != RotateDirection.Backward)
- {
- currentRotateDirection = RotateDirection.Forward;
- }
- else
- {
- currentRotateDirection = RotateDirection.None;
- }
- }
- Debug.Log("CurrentRotateDirection = " + currentRotateDirection);
- Debug.Log("----Slope Collider----");
- Debug.Log($"\tfwHit: {fwHit}");
- Debug.Log($"\trwHit: {rwHit}");
- if (currentRotateDirection == RotateDirection.Backward)
- {
-
- var angle = Mathf.Atan(distRw / distRwFw) * Mathf.Rad2Deg;
- bike.RotateAround(fwContact, frontWheelContactPoint.right, -angle);
- }
- else if (currentRotateDirection == RotateDirection.Forward)
- {
-
- var angle = Mathf.Atan(distFw / distRwFw) * Mathf.Rad2Deg;
- bike.RotateAround(rwContact, rearWheelContactPoint.right, angle);
- }
-
- }
-
- private SlopeHit? DrawRay(Vector3 start)
- {
- var layerMask = 1 << collisionLayer;
- RaycastHit hit;
-
- if (Physics.Raycast(start, -bike.up, out hit, 20f, layerMask))
- {
- Debug.DrawRay(start, -bike.up * hit.distance, Color.green);
-
- var isUneven = hit.collider.gameObject.CompareTag(StreetPartMetaTag.TAG_UNEVEN);
- var first = -(-bike.up * hit.distance);
- var second = (hit.normal);
- var angle = Mathf.Acos(Vector3.Dot(first, second) / first.magnitude * second.magnitude) * Mathf.Rad2Deg;
-
-
- return new SlopeHit(hit.distance, !isUneven, angle, hit.point);
- }
-
- if (Physics.Raycast(start, bike.up, out hit, 20f, layerMask))
- {
- Debug.DrawRay(start, -bike.up * hit.distance, Color.green);
-
- var isUneven = hit.collider.gameObject.CompareTag(StreetPartMetaTag.TAG_UNEVEN);
- var first = -(-bike.up * hit.distance);
- var second = (hit.normal);
- var angle = Mathf.Acos(Vector3.Dot(first, second) / first.magnitude * second.magnitude) * Mathf.Rad2Deg;
-
-
- return new SlopeHit(-hit.distance, !isUneven, angle, hit.point);
- }
-
- return null;
- }
- }
|