单位角度和位置相对方向
Unity angle and position relative to direction
我正在尝试围绕一个点创建一个 'star' 向量点,它们之间的角度与源和命中点之间的原始线(见图)我已经完成了创建与原始向量有小偏移的新向量:
private void FixedUpdate()
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 3000))
{
Vector3 mousePos = hit.point;
Debug.DrawLine(transform.position, hit.point, Color.yellow);
Vector3[] explorePoints = new Vector3[6] {
new Vector3 (hit.point.x - 1 , hit.point.y, hit.point.z + 1), // diag left
new Vector3 (hit.point.x + 1 , hit.point.y, hit.point.z + 1), // diag right
new Vector3 (hit.point.x - 1 , hit.point.y, hit.point.z), // left
new Vector3 (hit.point.x + 1 , hit.point.y, hit.point.z), // right
new Vector3 (hit.point.x - 1 , hit.point.y, hit.point.z - 1), // diag left back
new Vector3 (hit.point.x + 1 , hit.point.y, hit.point.z - 1), // diag right back
};
for (int x = 0; x < explorePoints.Length; x++)
{
Debug.DrawLine(mousePos, explorePoints[x], Color.red);
}
}
}
这在鼠标之间的角度接近 0 或 180 度时工作正常,但当然不是在其他角度:
我知道我可能需要四元数 class 来将球体和鼠标点之间的角度应用到方向向量,但我不太明白,例如
Quaternion q = Quaternion.FromToRotation(transform.position, mousePos);
for (int x = 0; x < explorePoints.Length; x++)
{
Debug.DrawLine(mousePos, q * explorePoints[x], Color.red);
}
如何始终使红线与黄线保持 n 角?
private void FixedUpdate()
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 3000))
{
Vector3 mousePos = hit.point;
Debug.DrawLine(transform.position, hit.point, Color.yellow);
Vector3 rayDir = transform.position - mousePos;
Vector3[] explorePoints = new Vector3[6] {
Quaternion.Euler(0, 0, 45) * rayDir.normalized,
Quaternion.Euler(0, 0, 90) * rayDir.normalized,
Quaternion.Euler(0, 0, 135) * rayDir.normalized,
Quaternion.Euler(0, 0, -45) * rayDir.normalized,
Quaternion.Euler(0, 0, -90) * rayDir.normalized,
Quaternion.Euler(0, 0, -135) * rayDir.normalized,
};
float starLength = 100;
for (int x = 0; x < explorePoints.Length; x++)
{
// we want to use the vector as DIRECTION, not point, hence mousePos + explorePoints[x] (starLength is just the length of the red line)
Debug.DrawLine(mousePos, mousePos + (explorePoints[x] * starLength), Color.red);
}
}
}
我正在尝试围绕一个点创建一个 'star' 向量点,它们之间的角度与源和命中点之间的原始线(见图)我已经完成了创建与原始向量有小偏移的新向量:
private void FixedUpdate()
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 3000))
{
Vector3 mousePos = hit.point;
Debug.DrawLine(transform.position, hit.point, Color.yellow);
Vector3[] explorePoints = new Vector3[6] {
new Vector3 (hit.point.x - 1 , hit.point.y, hit.point.z + 1), // diag left
new Vector3 (hit.point.x + 1 , hit.point.y, hit.point.z + 1), // diag right
new Vector3 (hit.point.x - 1 , hit.point.y, hit.point.z), // left
new Vector3 (hit.point.x + 1 , hit.point.y, hit.point.z), // right
new Vector3 (hit.point.x - 1 , hit.point.y, hit.point.z - 1), // diag left back
new Vector3 (hit.point.x + 1 , hit.point.y, hit.point.z - 1), // diag right back
};
for (int x = 0; x < explorePoints.Length; x++)
{
Debug.DrawLine(mousePos, explorePoints[x], Color.red);
}
}
}
这在鼠标之间的角度接近 0 或 180 度时工作正常,但当然不是在其他角度:
我知道我可能需要四元数 class 来将球体和鼠标点之间的角度应用到方向向量,但我不太明白,例如
Quaternion q = Quaternion.FromToRotation(transform.position, mousePos);
for (int x = 0; x < explorePoints.Length; x++)
{
Debug.DrawLine(mousePos, q * explorePoints[x], Color.red);
}
如何始终使红线与黄线保持 n 角?
private void FixedUpdate()
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 3000))
{
Vector3 mousePos = hit.point;
Debug.DrawLine(transform.position, hit.point, Color.yellow);
Vector3 rayDir = transform.position - mousePos;
Vector3[] explorePoints = new Vector3[6] {
Quaternion.Euler(0, 0, 45) * rayDir.normalized,
Quaternion.Euler(0, 0, 90) * rayDir.normalized,
Quaternion.Euler(0, 0, 135) * rayDir.normalized,
Quaternion.Euler(0, 0, -45) * rayDir.normalized,
Quaternion.Euler(0, 0, -90) * rayDir.normalized,
Quaternion.Euler(0, 0, -135) * rayDir.normalized,
};
float starLength = 100;
for (int x = 0; x < explorePoints.Length; x++)
{
// we want to use the vector as DIRECTION, not point, hence mousePos + explorePoints[x] (starLength is just the length of the red line)
Debug.DrawLine(mousePos, mousePos + (explorePoints[x] * starLength), Color.red);
}
}
}