| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- using UnityEngine;
- using System.Collections.Generic;
- public class ArrowCircleMotion : MonoBehaviour
- {
- public Transform targetPoint; // 圆心
- public float radius = 1f; // 半径
- public int arrowCount = 5; // 箭头数量
- public GameObject arrowPrefab; // 箭头预制体
- public float speed = 30f; // 旋转速度(度/秒)
- public Vector3 eulerRotation; // 圆的方向(圆的法线朝向)
- private List<Transform> arrows = new List<Transform>();
- private List<float> angles = new List<float>();
- private Quaternion circleRotation;
- public float modelForwardOffset = -90f; // 若模型默认朝 Y+
- void Start()
- {
- if (!arrowPrefab || !targetPoint) return;
- circleRotation = Quaternion.Euler(eulerRotation);
- for (int i = 0; i < arrowCount; i++)
- {
- GameObject arrow = Instantiate(arrowPrefab, transform);
- arrows.Add(arrow.transform);
- float angle = (360f / arrowCount) * i;
- angles.Add(angle);
- }
- }
- void Update()
- {
- circleRotation = Quaternion.Euler(eulerRotation);
- for (int i = 0; i < arrows.Count; i++)
- {
- // 更新角度
- angles[i] += -speed * Time.deltaTime;
- angles[i] %= 360f;
- float angle = angles[i];
- float selfRotateAngle = angle - 90f; // 自转角度,给Z轴旋转用(修正模型朝向)
- // 计算位置
- float rad = angle * Mathf.Deg2Rad;
- Vector3 localPos = new Vector3(Mathf.Cos(rad) * radius, Mathf.Sin(rad) * radius, 0f);
- Vector3 worldPos = targetPoint.position + circleRotation * localPos;
- arrows[i].position = worldPos;
- // 速度为负时,箭头绕Z轴旋转180度反向
- if (speed < 0f)
- {
- selfRotateAngle = angle + 90f;
- Quaternion flipRotation = Quaternion.AngleAxis(selfRotateAngle, Vector3.forward);
- arrows[i].rotation = circleRotation * flipRotation;
- }
- else
- {
- selfRotateAngle = angle - 90f;
- // 计算旋转(公转方向 + 自转修正)
- Quaternion zSelfRotation = Quaternion.AngleAxis(selfRotateAngle, Vector3.forward);
- arrows[i].rotation = circleRotation * zSelfRotation;
- }
-
- }
- }
- }
|