fraction
The fraction of the distance specified to the physics query before it detected a Collider2D.
Read time 1 minuteLast updated 4 days ago
Definition
- Type: Property
- Namespace: UnityEngine
- Assembly: UnityEngine.Physics2DModule
public float fraction { get; set; }
Remarks
When a physics query is used, it has a start position and an explicit end position or a direction and distance. The is a value in the range of and which is a representation of the returned RaycastHit2D.distance result (see code example).
fraction01If the physics query does not allow specifying an explicit end position or it has a default distance of then the is identical to the returned RaycastHit2D.distance and therefore will not be in the range of and .
infinityfraction01Additional Resources: RaycastHit2D.distance.
Examples
using UnityEngine;public class ExampleClass : MonoBehaviour{ public Vector2 direction; void Update() { // Cast a ray in the specified direction up to 10 world units away. RaycastHit2D hit = Physics2D.Raycast(transform.position, direction, 10f); // If we hit something then indicate if it was less-than or greater-than halfway of the physics query. if (hit) { if (hit.fraction < 0.5f) Debug.Log("Hit something before halfway!"); else Debug.Log("Hit something after halfway!"); } }}