rigidbody
The Rigidbody2D that the Collider2D detected by the physics query is attached to.
Read time 1 minuteLast updated 4 days ago
Definition
- Type: Property
- Namespace: UnityEngine
- Assembly: UnityEngine.Physics2DModule
public Rigidbody2D rigidbody { 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 Rigidbody2D the Collider2D is attached to.
colliderrigidbodyNOTE: is equivalent to using _attachedRigidbody and is provided for convenience only.
rigidbodyAdditional Resources: _collider, Rigidbody2D
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 and it was attached to a rigidbody then move the rigidbody to the world origin. if (hit && hit.rigidbody) hit.rigidbody.position = Vector2.zero; }}