Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


transform

The Transform on the GameObject that the Collider2D is attached to.
Read time 1 minuteLast updated 6 days ago

Definition

  • Type: Property
  • Namespace: UnityEngine
  • Assembly: UnityEngine.Physics2DModule
public Transform transform { get; }

Remarks

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 _collider is attached to.
NOTE:
transform
is equivalent to using _transform and is provided for convenience only. This field will be NULL if nothing was detected.
Additional Resources: _collider

Examples

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; }}