Version: Unity 6 Preview (6000.0)
LanguageEnglish
  • C#

RaycastHit2D.distance

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

public float distance;

Description

The distance the physics query traversed before it detected a Collider2D.

When the RaycastHit2D result is returned from a physics query, the distance refers to the distance from the physics query start position to the position the physics query shape intersected as indicated by RaycastHit2D.centroid. For simple linecast or raycast queries, RaycastHit2D.point is used to calculate the distance.

Additional resources: RaycastHit2D.fraction.

using UnityEngine;

public class ExampleClass : MonoBehaviour { public Vector2 direction;

void Update() { // Cast a ray in the specified direction. RaycastHit2D hit = Physics2D.Raycast(transform.position, direction);

// If we hit something that was less than 6 world units away then write a message. if (hit && hit.distance < 6f) Debug.Log("Hit something within range!"); } }