Version: Unity 6.1 Alpha (6000.1)
LanguageEnglish
  • C#

RaycastHit2D.rigidbody

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 Rigidbody2D rigidbody;

Description

The Rigidbody2D that the Collider2D detected by the physics query is attached to.

When the RaycastHit2D result is returned from a physics query, the collider refers to the specific Collider2D that was detected however rigidbody refers to the Rigidbody2D the Collider2D is attached to.

In the case where the Collider2D is not attached to a Rigidbody2D then rigidbody will be NULL.

NOTE: rigidbody is equivalent to using Collider2D.attachedRigidbody and is provided for convenience only.

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 and it was attached to a rigidbody then move the rigidbody to the world origin. if (hit && hit.rigidbody) hit.rigidbody.position = Vector2.zero; } }

Additional resources: Rigidbody2D class.

Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961