Version: 1.3
LanguageEnglish
  • C#

RaycastHit2D.centroid

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 Vector2 centroid;

Description

The centroid of the primitive used to perform the cast.

When the RaycastHit2D is returned from line or ray casting, the centroid is identical to the return point property. However, when using cast methods that use a geometry shape instead of a simple point such as circle or box casting, the centroid is the center of the respective shape when it is in contact with the returned point.

The centroid helps identify the position the cast shape should be in for it to collide at the contact point. The contact point takes into account any rotation specified for the shape when it was cast.

using UnityEngine;

public class Example : MonoBehaviour { // A stationary planet public GameObject planet; // The satellite is moving around the planet public GameObject satellite;

void Update() { RaycastHit2D hit = Physics2D.CircleCast(satellite.transform.position, 10.0f, planet.transform.position);

if (hit.collider != null) { // Draws a line from the planet to the centre of the satellite that was hit as the satellite moves Debug.DrawLine(planet.transform.position, hit.centroid, Color.yellow); } } }