unity--半圆包围posiotion

using UnityEngine;
using System.Collections;

public class NewMonoBehaviour : MonoBehaviour
{
    public int[] shipLevels; // 战舰等级数组
    public float radius; // 半圆包围阵型半径

    private Vector3[] attackPositions; // 攻击位置数组

    void Start()
    {
        // 初始化攻击位置数组
        attackPositions = new Vector3[shipLevels.Length];

        // 计算每个战舰的攻击位置
        for (int i = 0; i < shipLevels.Length; i++)
        {
            float angle = 360f * i / shipLevels.Length;
            float distance = GetAttackDistance(shipLevels[i]);
            Vector3 position = Quaternion.Euler(0f, angle, 0f) * Vector3.forward * distance;
            attackPositions[i] = position;
        }

        // 将攻击位置数组转换为世界坐标系
        for (int i = 0; i < attackPositions.Length; i++)
        {
            attackPositions[i] = transform.position + attackPositions[i];
        }

        // 在半圆上均匀分布攻击位置
        Vector3 center = transform.position;
        //Vector3 direction = Vector3.forward;
        int count = attackPositions.Length;
        for (int i = 0; i < count; i++)
        {
            float angle = i * Mathf.PI / count;
            float x = Mathf.Cos(angle);
            float z = Mathf.Sin(angle);
            Vector3 position = center + new Vector3(x, 0f, z) * radius;
            Vector3 toTarget = position - center;
            Vector3 toRotation = Vector3.forward;
            float rotationAngle = Vector3.Angle(toRotation, toTarget);
            Vector3 cross = Vector3.Cross(toRotation, toTarget);
            if (cross.y < 0)
            {
                rotationAngle = -rotationAngle;
            }
            Quaternion rotation = Quaternion.Euler(0f, rotationAngle, 0f);
            attackPositions[i] = position + rotation * attackPositions[i];
        }
    }

    float GetAttackDistance(int shipLevel)
    {
        switch (shipLevel)
        {
            case 1:
                return 10f;
            case 2:
            case 3:
            case 4:
                return 12f;
            case 5:
            case 6:
                return 14f;
            case 7:
            case 8:
                return 16f;
            default:
                return 0f;
        }
    }

}

你可能感兴趣的:(Unity,unity,游戏引擎,半圆坐标,半圆攻击)