Как разместить сферы в форме полукруга между 2 точками - PullRequest
0 голосов
/ 24 июня 2019

Я создаю маленькие сферы между двумя заданными точками.Однако он всегда генерируется по прямой линии.Возможно ли, что сферы могут начать генерировать как кривую из точки A в точку B?

Я определяю, сколько сфер следует генерировать между двумя точками, определяя NumberOfSegments.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GeneratePoints : MonoBehaviour
{
    public Transform PointA; 
    public Transform PointB; 
    public float NumberOfSegments = 3; 
    public float AlongThePath = .25f;

    // Update is called once per frame
    void Start()
    {
       StartCoroutine(StartSpheringOut());
    }

    IEnumerator StartSpheringOut()
    {
        NumberOfSegments += 1;// since we are skipping 1st placement since its the same as starting point we increase the number by 1 
        AlongThePath = 1 / (NumberOfSegments);//% along the path

        for (int i = 1; i < NumberOfSegments; i++)
        {
            yield return new WaitForSeconds(0.05f);
            Vector3 CreatPosition = PointA.position + (PointB.position - PointA.position) * (AlongThePath * i);

            GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
            sphere.transform.position = CreatPosition;
            sphere.transform.localScale = new Vector3(0.05f, 0.05f, 0.05f);
        }
    }
}

1 Ответ

1 голос
/ 24 июня 2019

На самом деле это почти уже существует в Руководстве по Unity :

using UnityEngine;
public class CircleFormation : MonoBehaviour
{
   // Instantiates prefabs in a circle formation
   public GameObject prefab;
   public int numberOfObjects = 20;
   public float radius = 5f;

   void Start()
   {
       for (int i = 0; i < numberOfObjects; i++)
       {
           float angle = i * Mathf.PI * 2 / numberOfObjects;
           float x = Mathf.Cos(angle) * radius;
           float z = Mathf.Sin(angle) * radius;
           Vector3 pos = transform.position + new Vector3(x, 0, z);
           float angleDegrees = -angle*Mathf.Rad2Deg;
           Quaternion rot = Quaternion.Euler(0, angleDegrees, 0);
           Instantiate(prefab, pos, rot);
       }
   }
}

. Вы можете использовать это для начинающих, а затем настроить его в соответствии со своими потребностями, чтобы заставить его работать в целом.Поэтому, поскольку вам нужна только половина круга, все, что вам нужно сделать, - это просто разделить angle на 2 и добавить pointA и pointB, чтобы вычислить центральную позицию.Кроме того, если обе позиции не находятся в одной плоскости XZ, вам необходимо повернуть весь круг:

public GameObject A;
public GameObject B;

public int amount;

[ContextMenu("PlaceSpheres()")]
privtae void DebugPlace()
{
    PlaceSpheres(A.transform.position, B.transform.position, amount);
}

public void PlaceSpheres(Vector3 posA, Vector3 posB, int numberOfObjects)
{
    // get circle center and radius
    var radius = Vector3.Distance(posA, posB) / 2f;
    var centerPos = (posA + posB) / 2f;

    // get a rotation that looks in the direction
    // posA -> posB
    var centerDirection = Quaternion.LookRotation((posB - posA).normalized);

    for (var i = 0; i < numberOfObjects; i++)
    {
        // Max angle is 180° (= Mathf.PI in rad) not 360° (= Mathf.PI * 2 in rad)
        //          |
        //          |    don't place the first object exactly on posA 
        //          |    but start already with an offset
        //          |    (remove the +1 if you want to start at posA instead)
        //          |               |
        //          |               |     don't place the last object on posB 
        //          |               |     but end one offset before
        //          |               |     (remove the +1 if you want to end 
        //          |               |     exactly a posB instead)
        //          |               |                       |
        //          V               V                       V
        var angle = Mathf.PI * (i + 1) / (numberOfObjects + 1f);
        var x = Mathf.Sin(angle) * radius;
        var z = Mathf.Cos(angle) * radius;
        var pos = new Vector3(x, 0, z);
        // Rotate the pos vector according to the centerDirection
        pos = centerDirection * pos;

        var sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
        sphere.transform.position = centerPos + pos;
        sphere.transform.localScale = new Vector3(0.05f, 0.05f, 0.05f);
    }
}

enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...