如何将球体放置在两点之间的半圆形中 [英] How to place spheres in a half circle shape between 2 points
问题描述
我正在两个给定点之间生成小球体.然而,它总是在一条直线上生成.球体是否有可能开始生成从 A 点到 B 点的曲线?
我通过定义NumberOfSegments
来定义两点之间应该生成多少个球体.
使用 System.Collections;使用 System.Collections.Generic;使用 UnityEngine;公共类 GeneratePoints : MonoBehaviour{公共变换点A;公共变换点B;公共浮动 NumberOfSegments = 3;公共浮动沿路径 = .25f;//每帧调用一次更新无效开始(){StartCoroutine(StartSpheringOut());}IEnumerator StartSpheringOut(){NumberOfSegments += 1;//因为我们跳过了第一个位置,因为它与起点相同,所以我们将数字增加 1沿路径 = 1/(NumberOfSegments);//沿路径百分比for (int i = 1; i
这实际上几乎已经存在于
I am generating small spheres between two given points. However it always generates in a straight line. Is it possible that the spheres can start generating as a curve from Point A to Point B?
I am defining how many spheres should be generated between two points by defining 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);
}
}
}
This actually almost exists already in the Unity Manual:
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);
}
}
}
You can use this for starters and then adjust it to your needs in order to make it work in general. So since you only want the half of the circle all you have to do is basically to devide the angle
by 2
and add the pointA
and pointB
in order to calculate the center position. Also if both positions are not in the same XZ-plane you would have to rotate the entire circle:
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);
}
}
这篇关于如何将球体放置在两点之间的半圆形中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!