物理查询在检测到 Collider2D 之前遍历的距离。
当 RaycastHit2D 结果从物理查询返回时,distance
指的是从物理查询起始位置到物理查询形状所指示的交叉位置的距离,如 RaycastHit2D.centroid 所示。对于简单的 linecast 或 raycast 查询,RaycastHit2D.point 用于计算距离。
其他资源: RaycastHit2D.fraction.
using UnityEngine;
public class ExampleClass : MonoBehaviour { public Vector2 direction;
void Update() { // Cast a ray in the specified direction. RaycastHit2D hit = Physics2D.Raycast(transform.position, direction);
// If we hit something that was less than 6 world units away then write a message. if (hit && hit.distance < 6f) Debug.Log("Hit something within range!"); } }