123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- using System;
- using Controller.Bicycle;
- using Roads;
- using UnityEngine;
- namespace Wheels
- {
- public class LerpSlopeCollider : MonoBehaviour
- {
- public int collisionLayer = 1;
- public Transform rearWheelContact;
- public Transform frontWheelContact;
- public Rigidbody bike;
- private const float TOLERANCE = 0.005f;
- private const float MAX_RAYCAST_LENGTH = 2f;
- private float distanceUntilCompletelyRotated;
- private Transform bikeTransform;
- private float currentFrontWheelSlope;
- private float currentFrontWheelMinY;
- private bool rotate;
- public int SlopeDirection { private set; get; } = 1;
- public delegate void OnSlopeChangedEvent(float timestamp, float slope);
- public event OnSlopeChangedEvent OnSlopeChanged;
- private void Start()
- {
- bikeTransform = bike.transform;
- distanceUntilCompletelyRotated = Vector3.Distance(frontWheelContact.position, rearWheelContact.position);
- }
- private void OnGUI()
- {
- GUI.TextField(new Rect(10, 200, 200, 20), $"Direction = {SlopeDirection}");
- GUI.TextField(new Rect(10, 220, 200, 20), $"MinY = {currentFrontWheelMinY}");
- GUI.TextField(new Rect(10, 250, 200, 20), $"rotation.x = {bikeTransform.localRotation.eulerAngles.x:n2}");
- GUI.TextField(new Rect(10, 270, 200, 20), $"Slope = {currentFrontWheelSlope}");
- }
- private void FixedUpdate()
- {
- var fwHit = CastRay(frontWheelContact.position, Vector3.down);
- if (fwHit != null)
- {
- if (Mathf.Abs(currentFrontWheelSlope - fwHit.SlopeDeg) > TOLERANCE)
- {
- rotate = true; //begin rotation
- currentFrontWheelSlope = fwHit.SlopeDeg;
- OnSlopeChanged?.Invoke(Time.fixedTime, currentFrontWheelSlope);
- }
- var minYDif = currentFrontWheelMinY - fwHit.MinY;
- if (Mathf.Abs(minYDif) > TOLERANCE)
- {
- SlopeDirection = -Math.Sign(minYDif);
- currentFrontWheelMinY = fwHit.MinY;
- }
- }
- RotateIfNeeded();
- }
- private IRoad CastRay(Vector3 pos, Vector3 direction)
- {
- var layerMask = 1 << collisionLayer;
- if (Physics.Raycast(pos, direction, out var hit, MAX_RAYCAST_LENGTH, layerMask))
- {
- Debug.DrawRay(pos, direction * hit.distance, Color.green);
- var roadHit = hit.collider.gameObject.GetComponentInParent<IRoad>();
- return roadHit;
- }
- Debug.DrawRay(pos, direction * MAX_RAYCAST_LENGTH, Color.white);
- return null;
- }
- private void RotateIfNeeded()
- {
- if (rotate)
- {
- var bikeAngle = bikeTransform.localRotation.eulerAngles.x;
- if (bikeAngle < -180)
- {
- bikeAngle = Mathf.Abs(360 + bikeAngle);
- }
- else if (bikeAngle > 180)
- {
- bikeAngle = Mathf.Abs(-360 + bikeAngle);
- }
- var slopeDif = SlopeDirection * (currentFrontWheelSlope - bikeAngle);
- if (Mathf.Abs(currentFrontWheelSlope - bikeAngle) <
- TOLERANCE)
- {
- rotate = false;
- return;
- }
- var forwardBikeSpeed = bikeTransform.InverseTransformDirection(bike.velocity).z;
- var rot = slopeDif * Time.fixedDeltaTime * forwardBikeSpeed / distanceUntilCompletelyRotated;
- if (slopeDif > 0) //lift front wheel
- {
- bikeTransform.RotateAround(frontWheelContact.position, -frontWheelContact.right,
- rot);
- }
- else //dip front wheel
- {
- bikeTransform.RotateAround(frontWheelContact.position, frontWheelContact.right,
- -rot);
- }
- }
- }
- }
- }
|