【发布时间】:2020-11-15 12:57:22
【问题描述】:
我有一个光线投射,每帧都基于 2 个点进行渲染,而这 2 个点每帧都会改变位置。
我需要的是一个不需要方向或多个对象的系统,而是需要 2 个点,然后实例化或销毁尽可能多的对象,以便将实例化的对象从一侧移到另一侧减去spaceBetweenPoints。如果您愿意,可以将其视为Angry Birds Style slingshot HUD,除了没有重力并且基于光线投射。
我的脚本:
public int numOfPoints; // The number of points that are generated (This would need to chnage based one the distance in the end)
public float spaceBetweenPoints; // The spacing between the generated points
private GameObject[] predictionPoints; // The prefab to be gernerated
private Vector2 firstPathStart; // The starting point for the raycast (Changes each frame)
private Vector2 firstPathEnd; // The ending point for the raycast (Changes each frame)
void start()
{
predictionPoints = new GameObject[numOfPoints];
for (int i = 0; i < numOfPoints; i++)
{
predictionPoints[i] = Instantiate(predictionPointPrefab, firePoint.position,
Quaternion.identity);
}
}
void Update
{
Debug.DrawLine(firstPathStart, firstPathEnd, UnityEngine.Color.black);
DrawPredictionDisplay();
}
void DrawPredictionDisplay()
{
for (int i = 0; i < numOfPoints; i++)
{
predictionPoints[i].transform.position = predictionPointPosition(i * spaceBetweenPoints);
}
}
Vector2 predictionPointPosition(float time)
{
Vector2 position = (Vector2)firstPathStart + direction.normalized * 10f * time;
return position;
}
当前系统只是简单地获取一个起始位置和一个方向,然后根据时间在该方向上移动预设数量的对象。这种做法也会导致问题,因为它是 endess 而不是只持续到光线投射结束:(请原谅我的绘画技巧)
Blue line = raycast
Black dots = instantiated prefab
Orange dot = raycast orange
Green dot = end of raycast
注意事项:
-
direction是我在编辑器中设置的动力,我需要它来整理我目前正在工作的内容,但在基于积分运行时应该没有必要。
【问题讨论】:
-
你能告诉我吗,1.这是3D还是2D。 2.我怎么知道
StartPoint和EndPoint的位置? -
@Ankit 当然,正如问题标签所说,它是 2D 的。光线投射的开始和结束位置由变量
firstPathStart和firstPathEnd确定,我根据每帧的其他不太相关的参数进行更改。还有其他问题我可以回答吗? -
这样就好了,我拿两个dummy object来代表Start和End位置
-
我制作了一个脚本,它为 List 中的所有点提供 Vector3 位置,然后你可以运行一个循环并 Instenciate 所有你想要的对象,这就足够了吗?
标签: c# unity3d 2d raycasting