如何使用 C# 在 Unity2D 中以特定半径围绕特定点生成对象
How to spawn object around a specific point with specific radius in Unity2D using C#
我试着做了一个类似于开锁游戏的游戏。但是,我很难在圆的边界周围随机生成一个点,但不在圆内。有关更多详细信息,我只想在圆圈的边界处生成一个游戏对象,而不是在圆圈中的每个位置。
请帮助我:<
非常感谢,祝你有美好的一天!
p/s: 我只是 unity 引擎和语法的初学者。如果你能给我一些关于如何自学统一的建议,那就太好了。非常感谢
你需要尝试一些东西并展示你是否遇到了困难。
我会开始尝试使用原始语言来理解点旋转以了解正在发生的事情,例如,普通 c# 中的点旋转:
// Rotate B around A by angle theta clockwise
private static (double x, double y) Rotate(
(double x, double y) A,
(double x, double y) B,
double theta)
{
double s = Math.Sin(theta);
double c = Math.Cos(theta);
// translate point back to origin:
B.x -= A.x;
B.y -= A.y;
// rotate point clockwise
double xnew = B.x * c - B.y * s;
double ynew = B.x * s + B.y * c;
// translate point back:
B.x = xnew + A.x;
B.y = ynew + A.y;
return B;
}
然后你可以检查一些基本的矢量运算和它们的数学背景,这并不难,看看用 unity 可以做什么 API:
Vector3.Reflect 和
Vector3.RotateRowards
当然你能想出办法。
首先,我非常喜欢点积。我在参考对象周围随机放置了一个位置(在本例中为 transform.position
)。然后在 randomPos
和参照物之间取方向。用 Vector3.Dot()
计算的点积。用点积的结果取 randomPos
和我们的 transform.forward
之间的角度。使用 Cos 和 Sin 函数计算 x 和 z 值。点积角总是给出 0 到 180 的值,所以我用 dotProductAngle
乘以 (Random.value > 0.5f ? 1f : -1f)
给 z-axis 随机性。如果不这样做,给定的随机位置将始终在玩家面前。
3D Space
private void SpawnSphereOnEdgeRandomly3D()
{
float radius = 3f;
Vector3 randomPos = Random.insideUnitSphere * radius;
randomPos += transform.position;
randomPos.y = 0f;
Vector3 direction = randomPos - transform.position;
direction.Normalize();
float dotProduct = Vector3.Dot(transform.forward, direction);
float dotProductAngle = Mathf.Acos(dotProduct / transform.forward.magnitude * direction.magnitude);
randomPos.x = Mathf.Cos(dotProductAngle) * radius + transform.position.x;
randomPos.z = Mathf.Sin(dotProductAngle * (Random.value > 0.5f ? 1f : -1f)) * radius + transform.position.z;
GameObject go = Instantiate(_spherePrefab, randomPos, Quaternion.identity);
go.transform.position = randomPos;
}
3D 示例
二维Space
private void SpawnSphereOnEdgeRandomly2D()
{
float radius = 3f;
Vector3 randomPos = Random.insideUnitSphere * radius;
randomPos += transform.position;
randomPos.y = 0f;
Vector3 direction = randomPos - transform.position;
direction.Normalize();
float dotProduct = Vector3.Dot(transform.forward, direction);
float dotProductAngle = Mathf.Acos(dotProduct / transform.forward.magnitude * direction.magnitude);
randomPos.x = Mathf.Cos(dotProductAngle) * radius + transform.position.x;
randomPos.y = Mathf.Sin(dotProductAngle * (Random.value > 0.5f ? 1f : -1f)) * radius + transform.position.y;
randomPos.z = transform.position.z;
GameObject go = Instantiate(_spherePrefab, randomPos, Quaternion.identity);
go.transform.position = randomPos;
}
二维示例
希望对您有所帮助
我试着做了一个类似于开锁游戏的游戏。但是,我很难在圆的边界周围随机生成一个点,但不在圆内。有关更多详细信息,我只想在圆圈的边界处生成一个游戏对象,而不是在圆圈中的每个位置。 请帮助我:< 非常感谢,祝你有美好的一天! p/s: 我只是 unity 引擎和语法的初学者。如果你能给我一些关于如何自学统一的建议,那就太好了。非常感谢
你需要尝试一些东西并展示你是否遇到了困难。 我会开始尝试使用原始语言来理解点旋转以了解正在发生的事情,例如,普通 c# 中的点旋转:
// Rotate B around A by angle theta clockwise
private static (double x, double y) Rotate(
(double x, double y) A,
(double x, double y) B,
double theta)
{
double s = Math.Sin(theta);
double c = Math.Cos(theta);
// translate point back to origin:
B.x -= A.x;
B.y -= A.y;
// rotate point clockwise
double xnew = B.x * c - B.y * s;
double ynew = B.x * s + B.y * c;
// translate point back:
B.x = xnew + A.x;
B.y = ynew + A.y;
return B;
}
然后你可以检查一些基本的矢量运算和它们的数学背景,这并不难,看看用 unity 可以做什么 API: Vector3.Reflect 和 Vector3.RotateRowards
当然你能想出办法。
首先,我非常喜欢点积。我在参考对象周围随机放置了一个位置(在本例中为 transform.position
)。然后在 randomPos
和参照物之间取方向。用 Vector3.Dot()
计算的点积。用点积的结果取 randomPos
和我们的 transform.forward
之间的角度。使用 Cos 和 Sin 函数计算 x 和 z 值。点积角总是给出 0 到 180 的值,所以我用 dotProductAngle
乘以 (Random.value > 0.5f ? 1f : -1f)
给 z-axis 随机性。如果不这样做,给定的随机位置将始终在玩家面前。
3D Space
private void SpawnSphereOnEdgeRandomly3D()
{
float radius = 3f;
Vector3 randomPos = Random.insideUnitSphere * radius;
randomPos += transform.position;
randomPos.y = 0f;
Vector3 direction = randomPos - transform.position;
direction.Normalize();
float dotProduct = Vector3.Dot(transform.forward, direction);
float dotProductAngle = Mathf.Acos(dotProduct / transform.forward.magnitude * direction.magnitude);
randomPos.x = Mathf.Cos(dotProductAngle) * radius + transform.position.x;
randomPos.z = Mathf.Sin(dotProductAngle * (Random.value > 0.5f ? 1f : -1f)) * radius + transform.position.z;
GameObject go = Instantiate(_spherePrefab, randomPos, Quaternion.identity);
go.transform.position = randomPos;
}
3D 示例
二维Space
private void SpawnSphereOnEdgeRandomly2D()
{
float radius = 3f;
Vector3 randomPos = Random.insideUnitSphere * radius;
randomPos += transform.position;
randomPos.y = 0f;
Vector3 direction = randomPos - transform.position;
direction.Normalize();
float dotProduct = Vector3.Dot(transform.forward, direction);
float dotProductAngle = Mathf.Acos(dotProduct / transform.forward.magnitude * direction.magnitude);
randomPos.x = Mathf.Cos(dotProductAngle) * radius + transform.position.x;
randomPos.y = Mathf.Sin(dotProductAngle * (Random.value > 0.5f ? 1f : -1f)) * radius + transform.position.y;
randomPos.z = transform.position.z;
GameObject go = Instantiate(_spherePrefab, randomPos, Quaternion.identity);
go.transform.position = randomPos;
}
二维示例
希望对您有所帮助