49 lines
1.4 KiB
C#
49 lines
1.4 KiB
C#
using UnityEngine;
|
|
|
|
public class BoatPlayerDetectSit : MonoBehaviour
|
|
{
|
|
[SerializeField]
|
|
private Boat boat;
|
|
|
|
[SerializeField]
|
|
private BoxCollider boxCollider;
|
|
|
|
public bool CheckPosition(Vector3 position, float margin = 0.1f)
|
|
{
|
|
Vector3 vector = base.transform.InverseTransformPoint(position);
|
|
Vector3 vector2 = boxCollider.size * 0.5f;
|
|
if (Mathf.Abs(vector.x) <= vector2.x - margin && Mathf.Abs(vector.y) <= vector2.y - margin)
|
|
{
|
|
return Mathf.Abs(vector.z) <= vector2.z - margin;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public Vector3 GetCorrection(Vector3 position, float margin = 0.1f)
|
|
{
|
|
Vector3 position2 = base.transform.InverseTransformPoint(position);
|
|
Vector3 vector = boxCollider.size * 0.5f;
|
|
position2.x = Mathf.Clamp(position2.x, 0f - vector.x + margin, vector.x - margin);
|
|
position2.y = Mathf.Clamp(position2.y, 0f - vector.y + margin, vector.y - margin);
|
|
position2.z = Mathf.Clamp(position2.z, 0f - vector.z + margin, vector.z - margin);
|
|
return base.transform.TransformPoint(position2);
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
if (boxCollider == null)
|
|
{
|
|
boxCollider = GetComponent<BoxCollider>();
|
|
}
|
|
boxCollider.isTrigger = true;
|
|
if (boxCollider.center != Vector3.zero)
|
|
{
|
|
Debug.LogError("BoatPlayerDetectSit BoxCollider center is not set to (0,0,0)");
|
|
}
|
|
if (base.transform.lossyScale != Vector3.one)
|
|
{
|
|
Debug.LogError("BoatPlayerDetectSit scale must be 1");
|
|
}
|
|
}
|
|
}
|