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 refers to the specific Collider2D that was detected however refers to the Transform on the GameObject that the RaycastHit2D.collider is attached to.
collidertransformNOTE: is equivalent to using transform and is provided for convenience only. This field will be NULL if nothing was detected.
transformAdditional Resources: RaycastHit2D.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; }}