【发布时间】:2020-11-14 22:16:51
【问题描述】:
我正在开发一款可以让 2D 弹丸从墙壁上弹开的游戏。我目前让物体本身弹跳得很好,但是我想要一个Angry Birds Style HUD(减去重力)来显示物体将要撞到墙上的位置,并显示它将以什么角度弹跳:(请原谅我的绘画技巧)
我有这个系统在技术方面工作正常,除了这个事实,因为光线投射是无穷无尽的,所以实际结果看起来像这样:
所以要明确一点,最后,我希望第一次 raycast 无限发出,直到它击中某物,然后当它生成第一个击中某物的第二次 raycast 时,让第二次 raycast 只发出一个预先指定的距离(可能是 3f 或类似的距离),或者直到它撞到某物,在这种情况下它应该停止。
我的脚本:
Transform firePoint;
void Update
{
DrawPredictionDisplay();
}
private void DrawPredictionDisplay()
{
Vector2 origin = firePoint.transform.position; //unity has a built in type converter that converts vector3 to vector2 by dropping the z component
direction = firePoint.transform.up;
float radius = 0.4f;
RaycastHit2D hit = Physics2D.CircleCast(origin, radius, direction);
// Draw black line from firepoint to hit point 1
Debug.DrawLine(origin, direction * 10000, UnityEngine.Color.black);
if (hit)
{
origin = hit.point + (hit.normal * radius);
Vector2 secondDirection = Vector2.Reflect(direction, hit.normal);
// Create second raycast
RaycastHit2D hit2 = Physics2D.CircleCast(origin, radius, secondDirection);
if (hit2)
{
if(hit.collider.gameObject.tag != "Destroy")
{
// Enable prediction points
for (int i = 0; i < numOfPoints; i++)
{
predictionPoints2[i].SetActive(true);
}
// Calculate reflect direction
Vector2 origin2 = hit2.point + (hit2.normal * radius);
// Draw blue line from hit point 1 to predicted reflect direction
Debug.DrawLine(origin, secondDirection * 10000, UnityEngine.Color.blue);
}
}
}
}
Vector2 predictionPointPosition(float time)
{
Vector2 position = (Vector2)firePoint.position + direction.normalized * 10f * time;
return position;
}
Vector2 predictionPointPosition2(float time, Vector2 origin, Vector2 direction)
{
Vector2 position = origin + direction.normalized * 10f * time;
return position;
}
注意事项:
虽然我会使用常规光线投射,但我发现普通光线投射不会削减它,因为光线投射只有 1 像素宽,而对象是(大约)512 像素乘 512 像素,这意味着对象会在光线投射之前物理接触墙壁确实,导致不准确。
我已经创建了一个系统,可以沿光线投射路径生成点,类似于愤怒的小鸟,以便玩家可以在游戏视图中看到光线投射在做什么,但因为它与我删除的问题无关我上面的脚本中的那部分代码。这意味着我需要做的就是限制光线投射的距离,而不是找到让玩家看到正在发生的事情的方法。 (我在注释中添加了这一点,以避免引发关于玩家是否可以看到正在发生的事情的对话。)
firepoint 是发射弹丸的武器的枪管/尖端。 (武器根据/跟随鼠标旋转)
【问题讨论】:
-
Debug.DrawLine将 2 个点作为参数,而不是点和方向,您的意思是使用Debug.DrawRay吗?你可以等到知道你是否撞到东西并且你有碰撞点/信息来绘制它之后再绘制路径。 -
@Pluto 我以前从未使用过 Debug.DrawRay,你能告诉我在这种情况下它会是什么样子吗?
-
Debug.DrawLine(origin, origin + direction * 10000, UnityEngine.Color.black);等价于Debug.DrawRay(origin, direction * 10000, UnityEngine.Color.black); -
@Pluto 好的,但是如何让它在撞到物体后停止?
标签: c# unity3d 2d raycasting projectile