и под ним находятся дочерние игровые объекты.
Я хочу повернуть 4 дочерних игровых объекта вместе вокруг целевую центральную точку.
скрипт заставляет их вращаться, но я не могу понять, как автоматически центрировать целевой объект в центре четырех объектов.на прикрепленном изображении вы можете увидеть в В верхнем левом окне сцены видно, что объект целевой сферы не находится в центре объектов с четырьмя кнопками. я хочу, чтобы скрипт автоматически центрировал цель.

Скрипт теперь состоит из 4 объектов, которые нормально вращаются вокруг цели, но цель не находится в центре.
Код: Выделить всё
using UnityEngine;
public class ButtonsRotator : MonoBehaviour
{
public float rotationSpeed = 30f; // Speed of rotation
public Transform target;
private Vector3 centerPoint;
void Start()
{
// Calculate the center point of all children
CalculateCenterPoint();
}
void Update()
{
foreach (Transform child in transform)
{
child.RotateAround(target.position, Vector3.up, rotationSpeed * Time.deltaTime);
}
}
private void CalculateCenterPoint()
{
if (transform.childCount == 0)
{
Debug.LogWarning("No children found under this GameObject.");
return;
}
// Calculate the center point based on the children positions
Vector3 totalPosition = Vector3.zero;
foreach (Transform child in transform)
{
totalPosition += child.position;
}
centerPoint = totalPosition / transform.childCount;
}
}
Код: Выделить всё
void Start()
{
// Calculate the center point of all children
CalculateCenterPoint();
target.position = centerPoint;
}
i думал использовать метод CalculateCenterPoint, чтобы установить цель в центре четырех объектов, но это не работает.
Подробнее здесь: https://stackoverflow.com/questions/791 ... s-together