Bullet-Hell 工具 (Unity C#) - 将物体/射弹的半圆旋转到定向 Vector2
Bullet-Hell Tool (Unity C#) - Rotate Half-Circle of Objects / Projectiles Towards Directional Vector2
_问候语
问候。我正在为 Unity 中的 'bullet-hell' 工具编写 c# 脚本,但我一直在尝试生成新月形/半圆形对象 - 转向行进方向。脚本每次都会生成一个完美的半圆并将对象保持在那里,无论对象数量如何。没问题,但是圆圈从来没有转向正确的方向。
_问题
我想要实现的是让半圆指向 centerObject 的行进方向。该行进方向通过 (1,1) 和 (-1,-1) 之间的 Vector2 描述。如何使用这个向量在行进方向转动半圆?
有多个 InitializeOrbit 实例,每个实例负责自己的半圆并具有相应的移动方向 Vector2。
_代码
在第一个脚本中,首先计算 'theta',然后生成对象。
public void InitializeOrbit()
{
for (int i = 0; i < numberOfOrbits; i++)
{
float theta = (1 * Mathf.PI / numberOfOrbits) * i;
Instantiate(OrbitPrefab, orbitLocation, transform.rotation);
}
}
而在第二种情况下,theta 用于计算各个物体的位置。
public void OrbitSource ()
{
Vector2 offset = new Vector2(Mathf.Sin(theta) * orbitRadius, Mathf.Cos(theta) * orbitRadius);
transform.position = (Vector2)centerTransform.position + offset;
}
希望我提供了正确的信息,在此先感谢。我已尝试阅读 - 但遗憾的是未能正确使用此信息来获得我想要的结果。
我设法自己写了一个解决方案。在中找到了很多资料,帮了大忙。这是脚本:
public void InitializeOrbitProjectiles()
{
Vector3 posA = new Vector3(projectile.direction.y, -projectile.direction.x) + transform.position * projectile.orbitRadius;
Vector3 posB = new Vector3(-projectile.direction.y, projectile.direction.x) + transform.position * projectile.orbitRadius;
var centerDirection = Quaternion.LookRotation((posB - posA).normalized);
float directionAngle = Angle(projectile.direction);
for (int i = 0; i < projectile.numberOfOrbits; i++)
{
float angle = (projectile.orbitCircle * Mathf.PI / projectile.numberOfOrbits) * i;
float x = Mathf.Sin(angle) * projectile.orbitRadius;
float z = Mathf.Cos(angle) * projectile.orbitRadius;
Vector3 pos = new Vector3(0, 0, 0);
Debug.Log("directionAngle " + directionAngle);
if ((directionAngle >= 0 && directionAngle < 90) || (directionAngle >= 271 && directionAngle < 360))
pos = new Vector3(0, x, z);
else if (directionAngle >= 90 && directionAngle < 91)
pos = new Vector3(x, 0, z);
else if (directionAngle >= 91 && directionAngle < 270)
pos = new Vector3(0, -x, z);
else if (directionAngle >= 270 && directionAngle < 271)
pos = new Vector3(-x, 0, z);
pos = centerDirection * pos;
Instantiate(projectileOrbitPrefab, transform.position + pos, transform.rotation);
}
}
以及 orbitSource 脚本:
public void OrbitSource()
{
transform.position = centerTransform.position + pos;
}
_问候语
问候。我正在为 Unity 中的 'bullet-hell' 工具编写 c# 脚本,但我一直在尝试生成新月形/半圆形对象 - 转向行进方向。脚本每次都会生成一个完美的半圆并将对象保持在那里,无论对象数量如何。没问题,但是圆圈从来没有转向正确的方向。
_问题
我想要实现的是让半圆指向 centerObject 的行进方向。该行进方向通过 (1,1) 和 (-1,-1) 之间的 Vector2 描述。如何使用这个向量在行进方向转动半圆?
有多个 InitializeOrbit 实例,每个实例负责自己的半圆并具有相应的移动方向 Vector2。
_代码
在第一个脚本中,首先计算 'theta',然后生成对象。
public void InitializeOrbit()
{
for (int i = 0; i < numberOfOrbits; i++)
{
float theta = (1 * Mathf.PI / numberOfOrbits) * i;
Instantiate(OrbitPrefab, orbitLocation, transform.rotation);
}
}
而在第二种情况下,theta 用于计算各个物体的位置。
public void OrbitSource ()
{
Vector2 offset = new Vector2(Mathf.Sin(theta) * orbitRadius, Mathf.Cos(theta) * orbitRadius);
transform.position = (Vector2)centerTransform.position + offset;
}
希望我提供了正确的信息,在此先感谢。我已尝试阅读
我设法自己写了一个解决方案。在
public void InitializeOrbitProjectiles()
{
Vector3 posA = new Vector3(projectile.direction.y, -projectile.direction.x) + transform.position * projectile.orbitRadius;
Vector3 posB = new Vector3(-projectile.direction.y, projectile.direction.x) + transform.position * projectile.orbitRadius;
var centerDirection = Quaternion.LookRotation((posB - posA).normalized);
float directionAngle = Angle(projectile.direction);
for (int i = 0; i < projectile.numberOfOrbits; i++)
{
float angle = (projectile.orbitCircle * Mathf.PI / projectile.numberOfOrbits) * i;
float x = Mathf.Sin(angle) * projectile.orbitRadius;
float z = Mathf.Cos(angle) * projectile.orbitRadius;
Vector3 pos = new Vector3(0, 0, 0);
Debug.Log("directionAngle " + directionAngle);
if ((directionAngle >= 0 && directionAngle < 90) || (directionAngle >= 271 && directionAngle < 360))
pos = new Vector3(0, x, z);
else if (directionAngle >= 90 && directionAngle < 91)
pos = new Vector3(x, 0, z);
else if (directionAngle >= 91 && directionAngle < 270)
pos = new Vector3(0, -x, z);
else if (directionAngle >= 270 && directionAngle < 271)
pos = new Vector3(-x, 0, z);
pos = centerDirection * pos;
Instantiate(projectileOrbitPrefab, transform.position + pos, transform.rotation);
}
}
以及 orbitSource 脚本:
public void OrbitSource()
{
transform.position = centerTransform.position + pos;
}