Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


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
fraction
is a value in the range of
0
and
1
which is a representation of the returned RaycastHit2D.distance result (see code example).
If the physics query does not allow specifying an explicit end position or it has a default distance of
infinity
then the
fraction
is identical to the returned RaycastHit2D.distance and therefore will not be in the range of
0
and
1
.
Additional 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!"); } }}