The results from performing any Cast query against the PhysicsWorld.
Use PhysicsWorld.CastGeometry or PhysicsWorld.CastRay to check whether a shape or ray will intersect other shapes as it moves through the world, for example to predict a collision before it happens.
Additional resources: PhysicsWorld.CastGeometry, PhysicsWorld.TestOverlapAABB, PhysicsAABB
// Check whether a small falling circle will collide with objects underneath it. using UnityEngine; using Unity.U2D.Physics; using Unity.Collections;
public class CastGeometryExample : MonoBehaviour { private PhysicsWorld world;
void Start() { world = PhysicsWorld.defaultWorld;
// Create a small falling circle. CircleGeometry object1Geometry = CircleGeometry.Create(0.5f, new Vector2(0.5f, 8f)); PhysicsBody object1 = world.CreateBody(new PhysicsBodyDefinition { position = object1Geometry.center, type = PhysicsBody.BodyType.Dynamic }); object1.CreateShape(object1Geometry);
// Set the translation and filter for the cast. Vector2 translation = new Vector2(0, -10f); PhysicsQuery.QueryFilter filter = new PhysicsQuery.QueryFilter();
// Cast the circle geometry through the world. // Use "using" to automatically dispose of the NativeArray once it goes out of scope. using NativeArray<PhysicsQuery.WorldCastResult> results = world.CastGeometry(object1Geometry, translation, filter, PhysicsQuery.WorldCastMode.Closest, Allocator.Temp);
if (results.Length > 0) { Debug.Log("Collision will occur!"); } } }
| Property | Description |
|---|---|
| fraction | The fraction of the query cast distance the shape would move to the point of detection, in the range [0, 1]. |
| isValid | Check if the result is valid. |
| normal | The surface normal at the point of contact. In all non-overlapped cases, this will be a unit-normal. If there was an initial overlap, the normal will be zero (degenerate) along with the PhysicsQuery.WorldCastResult.fraction being zero and PhysicsQuery.WorldCastResult.point being an arbitrary point in the overlapped region. See PhysicsQuery.WorldCastResult.point. |
| point | The point of contact. |
| shape | The shape that was detected by the cast. |