Cast
All the Collider2D shapes attached to the Rigidbody2D are cast into the Scene starting at each Collider position ignoring the Colliders attached to the same Rigidbody2D.
Read time 9 minutesLast updated 7 days ago
Definition
- Type: Method
- Namespace: UnityEngine
- Assembly: UnityEngine.Physics2DModule
Cast(Vector2, RaycastHit2D[], float)
All the Collider2D shapes attached to the Rigidbody2D are cast into the Scene starting at each Collider position ignoring the Colliders attached to the same Rigidbody2D.
public int Cast(Vector2 direction, RaycastHit2D[] results, float distance)
Parameters
Vector representing the direction to cast each Collider2D shape.
Array to receive results.
Maximum distance over which to cast the Collider(s).
Returns
Type | Description |
|---|---|
| int | Returns the number of results placed in the |
Remarks
This function will take all the Collider2D shapes attached to the Rigidbody2D and cast them into the Scene starting at the Collider position in the specified for an optional and return the results in the provided array.
directiondistanceresultsThe integer return value is the number of results written into the array. The results array will not be resized if it doesn't contain enough elements to report all the results. The significance of this is that no memory is allocated for the results and so garbage collection performance is improved when casts are performed frequently.
resultsAdditionally, this will also detect other Collider(s) overlapping the collider start position. In this case the cast shape will be starting inside the Collider and may not intersect the Collider surface. This means that the collision normal cannot be calculated in which case the collision normal returned is set to the inverse of the vector being tested.
directionNOTE: All results are sorted into ascending distance order i.e. the first result is the closest.
Cast(Vector2, List<RaycastHit2D>, float)
All the Collider2D shapes attached to the Rigidbody2D are cast into the Scene starting at each Collider position ignoring the Colliders attached to the same Rigidbody2D.
public int Cast(Vector2 direction, List<RaycastHit2D> results, float distance = Infinity)
Parameters
Vector representing the direction to cast each Collider2D shape.
List to receive results.
Maximum distance over which to cast the Collider(s).
Returns
Type | Description |
|---|---|
| int | Returns the number of results placed in the |
Remarks
This function will take all the Collider2D shapes attached to the Rigidbody2D and cast them into the Scene starting at the Collider position in the specified for an optional and return the results in the provided list.
directiondistanceresultsThe integer return value is the number of results written into the list. The results list will be resized if it doesn't contain enough elements to report all the results. This prevents memory from being allocated for results when the list does not need to be resized, and improves garbage collection performance when the query is performed frequently.
resultsresultsAdditionally, this will also detect other Collider(s) overlapping the collider start position. In this case the cast shape will be starting inside the Collider and may not intersect the Collider surface. This means that the collision normal cannot be calculated in which case the collision normal returned is set to the inverse of the vector being tested.
directionNOTE: All results are sorted into ascending distance order i.e. the first result is the closest.
Cast(Vector2, ContactFilter2D, RaycastHit2D[], float)
All the Collider2D shapes attached to the Rigidbody2D are cast into the Scene starting at each Collider position ignoring the Colliders attached to the same Rigidbody2D.
public int Cast(Vector2 direction, ContactFilter2D contactFilter, RaycastHit2D[] results, float distance = Infinity)
Parameters
Vector representing the direction to cast each Collider2D shape.
Filter results defined by the contact filter.
Array to receive results.
Maximum distance over which to cast the Collider(s).
Returns
Type | Description |
|---|---|
| int | Returns the number of results placed in the |
Remarks
This function will take all the Collider2D shapes attached to the Rigidbody2D and cast them into the Scene starting at the Collider position in the specified for an optional and return the results in the provided array.
directiondistanceresultsThe integer return value is the number of results written into the array. The results array will not be resized if it doesn't contain enough elements to report all the results. The significance of this is that no memory is allocated for the results and so garbage collection performance is improved when casts are performed frequently.
resultsThe parameter can filter the returned results by the options in ContactFilter2D.
contactFilterAdditionally, this will also detect other Collider(s) overlapping the collider start position. In this case the cast shape will be starting inside the Collider and may not intersect the Collider surface. This means that the collision normal cannot be calculated in which case the collision normal returned is set to the inverse of the vector being tested.
directionNOTE: All results are sorted into ascending distance order i.e. the first result is the closest.
Cast(Vector2, ContactFilter2D, List<RaycastHit2D>, float)
All the Collider2D shapes attached to the Rigidbody2D are cast into the Scene starting at each Collider position ignoring the Colliders attached to the same Rigidbody2D.
public int Cast(Vector2 direction, ContactFilter2D contactFilter, List<RaycastHit2D> results, float distance = Infinity)
Parameters
Vector representing the direction to cast each Collider2D shape.
Filter results defined by the contact filter.
List to receive results.
Maximum distance over which to cast the Collider(s).
Returns
Type | Description |
|---|---|
| int | Returns the number of results placed in the |
Remarks
This function will take all the Collider2D shapes attached to the Rigidbody2D and cast them into the Scene starting at the Collider position in the specified for an optional and return the results in the provided list.
directiondistanceresultsThe integer return value is the number of results written into the list. The results list will be resized if it doesn't contain enough elements to report all the results. This prevents memory from being allocated for results when the list does not need to be resized, and improves garbage collection performance when the query is performed frequently.
resultsresultsThe parameter can filter the returned results by the options in ContactFilter2D.
contactFilterAdditionally, this will also detect other Collider(s) overlapping the collider start position. In this case the cast shape will be starting inside the Collider and may not intersect the Collider surface. This means that the collision normal cannot be calculated in which case the collision normal returned is set to the inverse of the vector being tested.
directionNOTE: All results are sorted into ascending distance order i.e. the first result is the closest.
Cast(Vector2, float, Vector2, List<RaycastHit2D>, float)
The Rigidbody2D is cast into the Scene starting at the specified position and rotation. All the Collider2D shapes attached to the Rigidbody2D are cast along with it ignoring the Colliders attached to the same Rigidbody2D.
public int Cast(Vector2 position, float angle, Vector2 direction, List<RaycastHit2D> results, float distance = Infinity)
Parameters
The position to start casting the rigidbody from.
The rotation of the rigidbody.
Vector representing the direction to cast each Collider2D shape.
List to receive results.
Maximum distance over which to cast the Collider(s).
Returns
Type | Description |
|---|---|
| int | Returns the number of results placed in the |
Remarks
This function, unlike Rigidbody2D.Cast allows you to cast the Rigidbody2D and all its attached Collider2D through the scene but also allows the starting and of the Rigidbody2D to be specified.
positionangleThe integer return value is the number of results written into the list. The results list will be resized if it doesn't contain enough elements to report all the results. This prevents memory from being allocated for results when the list does not need to be resized, and improves garbage collection performance when the query is performed frequently.
resultsresultsNOTE: Additionally, this will also detect other Collider(s) overlapping the start position. In this case the cast shape will be starting inside another Collider and may not intersect the Collider surface. This means that the collision normal cannot be calculated in which case the collision normal returned is set to the inverse of the vector being tested.
directionNOTE: All results are sorted into ascending distance order i.e. the first result is the closest.
Additional Resources: Rigidbody2D.Cast and ContactFilter2D.
Cast(Vector2, float, Vector2, ContactFilter2D, List<RaycastHit2D>, float)
The Rigidbody2D is cast into the Scene starting at the specified position and rotation. All the Collider2D shapes attached to the Rigidbody2D are cast along with it ignoring the Colliders attached to the same Rigidbody2D.
public int Cast(Vector2 position, float angle, Vector2 direction, ContactFilter2D contactFilter, List<RaycastHit2D> results, float distance = Infinity)
Parameters
The position to start casting the rigidbody from.
The rotation of the rigidbody.
Vector representing the direction to cast each Collider2D shape.
Filter results defined by the contact filter.
List to receive results.
Maximum distance over which to cast the Collider(s).
Returns
Type | Description |
|---|---|
| int | Returns the number of results placed in the |
Remarks
This function, unlike Rigidbody2D.Cast allows you to cast the Rigidbody2D and all its attached Collider2D through the scene but also allows the starting and of the Rigidbody2D to be specified.
positionangleThis function also uses the specified therefore the specific LayerMask of each Collider2D attached to this Rigidbody2D is not used to detect contacts. If this isn't desired then the CastFrom overload without the ContactFilter2D argument should be used.
contactFilterThe integer return value is the number of results written into the list. The results list will be resized if it doesn't contain enough elements to report all the results. This prevents memory from being allocated for results when the list does not need to be resized, and improves garbage collection performance when the query is performed frequently.
resultsresultsNOTE: All results are sorted into ascending distance order i.e. the first result is the closest.
NOTE: Additionally, this will also detect other Collider(s) overlapping the start position. In this case the cast shape will be starting inside another Collider and may not intersect the Collider surface. This means that the collision normal cannot be calculated in which case the collision normal returned is set to the inverse of the vector being tested.
directionAdditional Resources: Rigidbody2D.Cast and ContactFilter2D.