-
-
Notifications
You must be signed in to change notification settings - Fork 73
Ray2D 2D射线
YHH edited this page Sep 16, 2020
·
1 revision
框架提供的Ray2D 虽然从技术上讲不是射线(射线只是起点和方向),但它既是直线又是射线,因此起着双重作用。
- raycast是否检测配置为触发器的碰撞器
// 默认为 false
Physics.raycastsHitTriggers = true;
- 在碰撞器中开始的射线/直线是否强制转换检测到那些碰撞器
// 默认为 false
Physics.raycastsStartInColliders = true;
rayIntersects(ray: Ray2D, distance: Ref<number>): boolean
针对一个矩形,我们可以用射线来检测是否相交
let rect = new Rectangle(0, 0, 30, 30);
// 射线的起始坐标和结束坐标
let ray = new Ray2D(new Vector2(0, 0), new Vector2(640, 1136));
// 定义距离 这个值是为了返回射线到矩形的距离
let distance = new Ref<number>(0);
let intersects = rect.rayIntersects(ray, distance);
linecast(start: Vector2, end: Vector2, layerMask: number = Physics.allLayers): RaycastHit
返回与layerMask匹配的碰撞器的第一次命中的结果
let hit = Physics.linecast(new Vector2(10, 10), new Vector2(640, 1136));
// hit为碰撞结果 如果为null则无碰撞
// 碰撞到的collider
let collider = hit.collider;
// 撞击发生时沿射线的距离的分数
let fraction = hit.fraction;
// 从射线原点到碰撞点的距离
let distance = hit.distance;
// 在世界空间中射线击中碰撞器表面点的坐标
let point = hit.point;
// 被射线击中表面的法向量
let normal = hit.normal;
// 质心, 被击中物体的形状的位置
let centroid = hit.centroid;
boxToBoxCast(first: Box, second: Box, movement: Vector2, hit: RaycastHit): boolean
let firstBox = new Box(100, 100);
let secondBox = new Box(200, 150);
// 用于返回的结果
let hit = new RaycastHit();
let cast = ShapeCollisions.boxToBoxCast(firstBox, secondBox, new Vector2(1, 1), hit);