【发布时间】:2021-08-19 12:32:54
【问题描述】:
我正在尝试在 Unity 中制作一把枪,它可以用 Line 发射 Raycast。 (在 2d 中)我使用玩家位置作为原点,鼠标位置作为方向。这在有碰撞检测时非常有效,但是当没有碰撞时,我必须手动找到方向并计算终点,因为如果玩家错过了射门,我仍然希望线渲染。这是我找到这个端点的代码:
//finding the position of Mouse in worldspace
Vector3 mousePos = CameraController.mouseToWorld(Input.mousePosition);
mousePos.z = 0;
//Calculating whether or not the raycast is a hit
RaycastHit2D Ray = Physics2D.Raycast(Player.transform.position,mousePos,distance);
if(Ray.collider != null)
{
PassTheHit(Ray, Ray.collider.gameObject.name);
point = Ray.point;
}
else
{
//Calculating the farthest point on the line. this does not work.
point = (transform.position - mousePos).normalized;
point *= -1 * (distance / 2);
point.z = 0f;
}
特别注意 else{} 语句。找到该点的计算不起作用。它太长了,而且没有朝正确的方向射击。
非常感谢任何帮助。
【问题讨论】: