【问题标题】:How do I raycast mouse positions for the last 0.25 seconds如何在最后 0.25 秒内对鼠标位置进行光线投射
【发布时间】:2017-12-22 09:59:13
【问题描述】:

我想对鼠标移动的最后 0.25 秒进行光线投射。

任何超过 0.25 秒的移动都应该从我的队列中删除。

目前,我使用最大大小(大小)的队列。当它超过最大大小时,它开始删除条目。如何将此方法转换为删除早于 0.25f 秒的条目?

    //Ray Cache
    public Queue<Ray> inputRays;
    int counter = 0;
    int size = 10;//size of queue

/**********************************************/

private void Start()
{
    inputRays = new Queue<Ray>();
}

private void FixedUpdate()
{
    QueueInputRays();
}


  private void QueueInputRays()
    {
        if (counter < size)
        {
            inputRays.Enqueue(Camera.main.ScreenPointToRay(Input.mousePosition));
            counter += 1;
        }
        else
        {
            inputRays.Enqueue(Camera.main.ScreenPointToRay(Input.mousePosition));
            inputRays.Dequeue();
        }
    }

【问题讨论】:

  • 尝试解决此问题的一种方法不是直接使用射线,而是使用一个新类,该类将包含射线和射线投射的时间。然后,查看队列前面的实例并将该时间与现在的时间进行比较,因此如果时间大于 0.25,则将该实例出列。

标签: c# unity3d game-physics raycasting


【解决方案1】:

如果您要使用 FixedUpdate,那么您可以简单地添加到队列中一定数量。入队直到达到一定的计数,然后出队和入队。

例如,如果您知道 0.02 是增量时间,那么您需要

0.25 / 0.02 = 12.5

将其四舍五入到 12 并且:

private Queue<Ray>queue = new Queue<Ray>();
public void AddToQueue(Ray ray)
{
   if(this.queue.Count > 12){ this.queue.Dequeue(); }
   this.queue.Enqueue(ray);
}
public Ray[] GetRays()
{ 
    return this.queue.ToArray();
} 

这将使事情变得更简单,因为您不必跟踪计时器。也就是说,如果 FixedUpdate 能够以定义的速度运行。

【讨论】:

    猜你喜欢
    • 2014-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-12
    • 1970-01-01
    • 2019-03-10
    相关资源
    最近更新 更多