This is more efficient than checking for specific tags, because it lets you filter out false positives at the source. This function is most commonly used in first-person shooter games, where the main camera (the character) is the ray’s origin (starting point. The purpose of the ray (vector) is to determine if it intersects with any colliders or other game objects. QueryTriggerInteraction.Ignore // Ignore trigger colliders Ray Casting is forming a line or vector from a specific point to another point in a 3D plane. RaycastLayers, // Only hit colliders on these layers Out hitInfo, // Destination for hit informationįloat.positiveInfinity, // Change this to limit the ray length Then you can modify your raycast like so: bool hitSomething = Physics.Raycast( You can expose a public LayerMask raycastLayers variable in your script, so you can choose in the Inspector which physics layers you're interested in. If you don't want to search every collider in your scene, one of the best ways to control the raycast is to use Physics Layers. HitInfo.rigidbody will give you the physics Rigidbody that owns the collider you hit, if it was a dynamic or kinematic object. Your script uses this to orient the buildings to point directly out from the ground, instead of diagonal to the surface. HitInfo.normal will tell you the direction that the surface of the collider is facing at the point you hit. HitInfo.point will tell you the position in world space where the ray hit the surface of the collider. You can use this to get the hit object's GameObject and check its tag with ("someTag"), for example. llider will give you the Collider component that the ray hit. If it finds a collider that's in the path of the travelling point, it returns "true," meaning "yes, I found a hit." If you've provided an out RaycastHit parameter, it will store information about the first hit - the closest object along the ray.įrom that RaycastHit, you can learn a lot about the intersection: (This is a simplification - in reality the engine has lots of clever acceleration structures and math so it can do this efficiently without looking at every collider or checking every point along the line one by one) As it goes, it searches through all the physics colliders in your scene to check whether the point hits any of them during its travel. The physics engine takes this point and sweeps it along that direction. It takes a starting point and a direction as input (together, they make a "ray"). A raycast is like shining a laser pointer in your scene, to see what the laser hits.
0 Comments
Leave a Reply. |