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

RaycastHit2D.transform

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 Transform transform;

Description

The Transform on the GameObject that the Collider2D is attached to.

When the RaycastHit2D result is returned from a physics query, the collider refers to the specific Collider2D that was detected however transform refers to the Transform on the GameObject that the RaycastHit2D.collider is attached to.

NOTE: transform is equivalent to using Collider2D.transform and is provided for convenience only. This field will be NULL if nothing was detected.

Additional resources: RaycastHit2D.collider.

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 something was hit then move the transform to the world origin. if (hit) hit.transform.position = Vector3.zero; } }