Version: 2022.3
public Vector2 centroid ;

描述

用于执行投射的图元的质心。

从线或射线投射返回 RaycastHit2D 时,质心与返回点属性相同。但是当使用利用几何形状(而不是简单点)的投射方法(如圆形或盒体投射)时,centroid 是相应形状在与返回点接触时的中心。

centroid 可用于确定投射形状若要在接触点处碰撞而应该处于的位置。请注意,该点会考虑在投射形状时为形状指定的任何旋转。

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