123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- using System;
- using UnityEngine;
- [RequireComponent(typeof(BoxCollider))]
- public class SlopeCollider : MonoBehaviour
- {
- public int collisionLayer = 11;
- private readonly ContactPoint[] contacts = new ContactPoint[4];
- private Transform t;
- private BoxCollider bikeCollider;
- private void Start()
- {
- t = transform;
- bikeCollider = GetComponent<BoxCollider>();
- }
- private void OnCollisionStay(Collision other)
- {
- if (other.gameObject.layer == collisionLayer)
- {
- var numberOfContacts = other.GetContacts(this.contacts);
- //Debug.Log($"Has {numberOfContacts} contacts with {other.gameObject.name}");
- /*foreach (var c in contacts)
- {
- Debug.DrawLine(c.point, c.point + c.normal * 0.5f, Color.red);
- }*/
- var bounds = bikeCollider.bounds;
- var leftBackCorner = t.TransformPoint(bikeCollider.center - bikeCollider.size / 2);
- var localExtends = bikeCollider.size;
- localExtends.Scale(t.localScale);
- var rightBackCorner = leftBackCorner + t.right * localExtends.x;
- var forward = t.forward;
- var leftFrontCorner = leftBackCorner + forward * localExtends.z;
- var rightFrontCorner = rightBackCorner + forward * localExtends.z;
- var minSide = Helpers.GetMinComponent(localExtends);
- Debug.DrawLine(leftBackCorner, rightBackCorner, Color.red);
- Debug.DrawLine(leftBackCorner, leftBackCorner + t.up * localExtends.y, Color.green);
- Debug.DrawLine(leftBackCorner, leftFrontCorner, Color.blue);
-
- foreach(var c in contacts)
- {
- if ((leftBackCorner - c.point).magnitude < minSide/2.2f)
- {
- Debug.Log("Hit LeftBackCorner");
- }
- else if ((rightBackCorner - c.point).magnitude < minSide/2.2f)
- {
- Debug.Log("Hit RightBackCorner");
- }
- else if ((leftFrontCorner - c.point).magnitude < minSide/2.2f)
- {
- Debug.Log("Hit LeftFrontCorner");
- }
- else if ((rightFrontCorner - c.point).magnitude < minSide/2.2f)
- {
- Debug.Log("Hit RightFrontCorner");
- }
- }
- //Debug.Log($"Bounds = {bounds}");
- //Debug.Log($"LocalCenter = {localCenter}, localExtends = {localExtends}");
- }
- }
- }
|