
コライダーのClosestPointOnBoundsとClosestPointの違いを調べてみました。Boxコライダーだとわからなかったので、上のようにカプセルコライダーを使いました。
FPSプレイヤーの位置を設定し、ClosestPointには赤、ClosestPointOnBoundsには青の球を置いてみました。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ColliderTest : MonoBehaviour
{
[SerializeField] Transform player;
[SerializeField] Collider coll;
[SerializeField] GameObject redSphere;
[SerializeField] GameObject blueSphere;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.E))
{
// Sphereを削除
foreach(GameObject sphere in GameObject.FindGameObjectsWithTag("Sphere"))
{
Destroy(sphere);
}
// ClosestPoint
Instantiate(redSphere, coll.ClosestPoint(player.position), redSphere.transform.rotation);
// ClosestPointOnBounds
Instantiate(blueSphere, coll.ClosestPointOnBounds(player.position), blueSphere.transform.rotation);
}
}
}
真上から見ると、ClosestPointはコライダーの表面で、ClosestPointOnBoundsはコライダーがすっぽり入るバウンディングボックスの表面にあるのがわかります。
