/// <summary> /// Performs a sweep into the scene using a box and returns the closest found hit, if any. /// </summary> /// <param name="box">Box to sweep through the scene.</param> /// <param name="rotation">Orientation of the box.</param> /// <param name="unitDir">Unit direction towards which to perform the sweep.</param> /// <param name="hit">Information recorded about a hit. Only valid if method returns true.</param> /// <param name="layer">Layers to consider for the query. This allows you to ignore certain groups of objects. /// </param> /// <param name="max">Maximum distance at which to perform the query. Hits past this distance will not be detected. /// </param> /// <returns>True if something was hit, false otherwise.</returns> public static bool BoxCast(AABox box, Quaternion rotation, Vector3 unitDir, out PhysicsQueryHit hit, ulong layer = ulong.MaxValue, float max = float.MaxValue) { ScriptPhysicsQueryHit scriptHit = new ScriptPhysicsQueryHit(); if(Internal_BoxCast(ref box, ref rotation, ref unitDir, out scriptHit, layer, max)) { ConvertPhysicsQueryHit(ref scriptHit, out hit); return true; } hit = new PhysicsQueryHit(); return false; }
private static extern bool Internal_SphereCast(ref Sphere sphere, ref Vector3 unitDir, out ScriptPhysicsQueryHit hit, ulong layer, float max);
private static extern bool Internal_RayCast(ref Vector3 origin, ref Vector3 unitDir, out ScriptPhysicsQueryHit hit, ulong layer, float max);
private static extern bool Internal_CapsuleCast(ref Capsule capsule, ref Quaternion rotation, ref Vector3 unitDir, out ScriptPhysicsQueryHit hit, ulong layer, float max);
private static extern bool Internal_ConvexCast(IntPtr mesh, ref Vector3 position, ref Quaternion rotation, ref Vector3 unitDir, out ScriptPhysicsQueryHit hit, ulong layer, float max);
/// <summary> /// Converts all provided physics query hit infos retrieved from native code into managed physics query hits. /// </summary> /// <param name="scriptHits">Native physics query hits.</param> /// <returns>Converted managed physics query hits.</returns> private static PhysicsQueryHit[] ConvertPhysicsQueryHits(ScriptPhysicsQueryHit[] scriptHits) { PhysicsQueryHit[] output = new PhysicsQueryHit[scriptHits.Length]; for (int i = 0; i < scriptHits.Length; i++) ConvertPhysicsQueryHit(ref scriptHits[i], out output[i]); return output; }
private static extern bool Internal_BoxCast(ref AABox box, ref Quaternion rotation, ref Vector3 unitDir, out ScriptPhysicsQueryHit hit, ulong layer, float max);
/// <summary> /// Converts a physics query hit info retrieved from native code into managed physics query hit. /// </summary> /// <param name="scriptHit">Native physics query hit info.</param> /// <param name="hit">Managed physics query hit info</param> private static void ConvertPhysicsQueryHit(ref ScriptPhysicsQueryHit scriptHit, out PhysicsQueryHit hit) { if (scriptHit.collider != null) hit.collider = scriptHit.collider.Component; else hit.collider = null; hit.distance = scriptHit.distance; hit.normal = scriptHit.normal; hit.point = scriptHit.point; hit.triangleIdx = scriptHit.triangleIdx; hit.uv = scriptHit.uv; }
/// <summary> /// Performs a sweep into the scene using a sphere and returns the closest found hit, if any. /// </summary> /// <param name="sphere">Sphere to sweep through the scene.</param> /// <param name="unitDir">Unit direction towards which to perform the sweep.</param> /// <param name="hit">Information recorded about a hit. Only valid if method returns true.</param> /// <param name="layer">Layers to consider for the query. This allows you to ignore certain groups of objects. /// </param> /// <param name="max">Maximum distance at which to perform the query. Hits past this distance will not be detected. /// </param> /// <returns>True if something was hit, false otherwise.</returns> public static bool SphereCast(Sphere sphere, Vector3 unitDir, out PhysicsQueryHit hit, ulong layer = ulong.MaxValue, float max = float.MaxValue) { ScriptPhysicsQueryHit scriptHit = new ScriptPhysicsQueryHit(); if(Internal_SphereCast(ref sphere, ref unitDir, out scriptHit, layer, max)) { ConvertPhysicsQueryHit(ref scriptHit, out hit); return true; } hit = new PhysicsQueryHit(); return false; }
/// <summary> /// Performs a sweep into the scene using a convex mesh and returns the closest found hit, if any. /// </summary> /// <param name="mesh">Mesh to sweep through the scene. Must be convex.</param> /// <param name="position">Starting position of the mesh.</param> /// <param name="rotation">Orientation of the mesh.</param> /// <param name="unitDir">Unit direction towards which to perform the sweep.</param> /// <param name="hit">Information recorded about a hit. Only valid if method returns true.</param> /// <param name="layer">Layers to consider for the query. This allows you to ignore certain groups of objects. /// </param> /// <param name="max">Maximum distance at which to perform the query. Hits past this distance will not be detected. /// </param> /// <returns>True if something was hit, false otherwise.</returns> public static bool ConvexCast(PhysicsMesh mesh, Vector3 position, Quaternion rotation, Vector3 unitDir, out PhysicsQueryHit hit, ulong layer = ulong.MaxValue, float max = float.MaxValue) { IntPtr meshPtr = IntPtr.Zero; if (mesh != null) meshPtr = mesh.GetCachedPtr(); ScriptPhysicsQueryHit scriptHit = new ScriptPhysicsQueryHit(); if(Internal_ConvexCast(meshPtr, ref position, ref rotation, ref unitDir, out scriptHit, layer, max)) { ConvertPhysicsQueryHit(ref scriptHit, out hit); return true; } hit = new PhysicsQueryHit(); return false; }
private static extern bool Internal_RayCast(IntPtr thisPtr, ref Vector3 origin, ref Vector3 unitDir, out ScriptPhysicsQueryHit hit, float maxDist);
public bool Raycast(Vector3 origin, Vector3 unitDir, out ScriptPhysicsQueryHit hit, float maxDist) { return Internal_RayCast(mCachedPtr, ref origin, ref unitDir, out hit, maxDist); }