【发布时间】:2020-05-01 02:09:32
【问题描述】:
我通过光线投射到地形表面来程序性地生成草四边形, 为了让它们看起来更有机,我想在 Z 上添加一个随机旋转,但我的每一次尝试都会完全疯狂地旋转草块。
没有随机的 z 旋转,草块在地形上很好,但对齐感觉不是很有机
void RaycastItem(ItemChunk itemChunk)
{
RaycastHit hit;
Vector3 randPosition = new Vector3(Random.Range(-gridSize/2, gridSize/2), -20, Random.Range(-gridSize/2, gridSize/2)) + itemChunk.coord;
if (Physics.Raycast(randPosition, Vector3.down, out hit, raycastDistance))
{
Quaternion spawnRotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
Vector3 overlapTestBoxScale = new Vector3(overlapTestBoxSize, overlapTestBoxSize, overlapTestBoxSize);
Collider[] collidersInsideOverlapBox = new Collider[1];
int numberOfCollidersFound = Physics.OverlapBoxNonAlloc(hit.point, overlapTestBoxScale, collidersInsideOverlapBox, spawnRotation, spawnedObjectLayer);
if (numberOfCollidersFound == 0)
{
Pick(itemChunk, hit.point, spawnRotation);
}
}
}
void Pick(ItemChunk itemChunk, Vector3 positionToSpawn, Quaternion rotationToSpawn)
{
int randomIndex = Random.Range(0, itemsToPickFrom.Length);
GameObject item = itemsToPickFrom[randomIndex];
// this is one of the places I tried applying the random rotation
rotationToSpawn *= Quaternion.Euler(item.transform.rotation.x, item.transform.rotation.y, item.transform.rotation.z + Random.Range(0, 360));
GameObject clone = Instantiate(item, positionToSpawn, rotationToSpawn);
clone.transform.parent = itemChunk.transform;
Debug.Log(clone.transform.localScale);
}
我的四元数转换有问题吗?这是实例化特有的吗? 对于实际问题,我不得不将 Quad 放入 Empty 中来制作预制件,Quad 有 0 个旋转/变换,Quad 有 90 度旋转。
【问题讨论】:
标签: c# unity3d rotation quaternions raycasting