问题描述
我在两个给定点之间生成了小球体.但是,它总是沿直线产生.球体是否可能开始以点A到点B的曲线形式生成?
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?
我通过定义NumberOfSegments
来定义两点之间应生成多少个球体.
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);
}
}
}
推荐答案
这实际上已经存在于统一手册:
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平面中,则必须旋转整个圆:
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);
}
}
这篇关于如何在两个点之间以半圆形放置球体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!