c# - 一旦CircleCast在Unity中碰到某些东西就停止它?
问题描述
我正在开发一款 2D 弹丸可以从墙上弹射的游戏。我目前让物体本身弹跳得很好,但是我想要一个愤怒的小鸟风格的 HUD(减去重力)来显示物体将要撞到墙上的位置,并显示它会以什么角度弹跳:(请原谅我的绘画技巧)
我让这个系统在技术方面运行良好,除了因为光线投射是无止境的,所以实际结果如下所示:
所以要明确一点,最后,我希望第一个射线投射无限地出去,直到它击中某物,然后当它生成第一个击中某物的第二个第二个射线投射时,让第二个射线投射只发出预先指定的距离(可能是 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 像素 x 512 像素,这意味着对象会在光线投射之前物理接触墙壁,导致不准确。
我已经创建了一个系统,该系统沿光线投射路径生成点,类似于愤怒的小鸟,以便玩家可以在游戏视图中看到光线投射在做什么,但由于它与我删除的问题无关上面我的脚本中的代码。这意味着我需要做的就是限制光线投射的距离,而不是找到让玩家看到正在发生的事情的方法。(我在注释中添加了这一点,以避免引发关于玩家是否可以看到正在发生的事情的对话。)
firepoint
是发射弹丸的武器的枪管/尖端。(武器根据/跟随鼠标旋转)
解决方案
在知道它是否撞到物体后,您可以绘制线/路径:
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);
var firstPathStart = origin;
var firstPathEnd = origin + direction * 10000;
if (hit)
{
origin = hit.point + (hit.normal * radius);
firstPathEnd = origin;
Vector2 secondDirection = Vector2.Reflect(direction, hit.normal);
var secondPathStart = firstPathEnd;
var secondPathEnd = origin + secondDirection * 3f;
// 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);
secondPathEnd = origin2;
}
}
// Draw blue line from hit point 1 to predicted reflect direction
Debug.DrawLine(secondPathStart, secondPathEnd, UnityEngine.Color.blue);
}
// Draw black line from firepoint to hit point 1
Debug.DrawLine(firstPathStart, firstPathEnd, UnityEngine.Color.black);
您可以创建一个类来存储路径信息(路径有多少段,每个段的开始和结束位置),在检查碰撞时填充并在最后绘制它。
推荐阅读
- java - ASM 字节码覆盖方法作用域 var 接近方法结束
- java - 如何从具有键值对的csv中删除重复行?
- unity3d - 在 Unity 中测量帧大小
- excel - 列中的日期范围检查之间
- c++ - 带有 std::vector 的 MPI_Scatter 和 MPI_Gather
- javascript - 在我的 JavaScript 函数中看不到全局变量
- c# - 野田时间:如何表示“标准时间”
- java - 具有自定义值和默认值的枚举
- node.js - 如何在不影响 Node.JS 应用程序的情况下从后台异步发送大量电子邮件?
- angular - 如何修复错误:没有可用于依赖类型的模块工厂:ContextElementDependency