解决不准确重播系统的方法 [英] Approach to solve a not accurate replay system

查看:32
本文介绍了解决不准确重播系统的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试在 unity 和 c# 中制作准确的回放系统

Trying to make accurate replay system in unity and c#

大家好,我正在开发一款赛车游戏,我决定添加一个重播系统以允许幽灵车",最初我在某些事件中记录数据,例如按键,但只在所有帧中记录该数据我管理一个流畅的重播,而且它仍然好的,因为文件不是很大并且重放工作,但问题是时间总是有轻微的变化,最多只有 0.1 秒或 0.2,我有一个关键帧列表,在每个位置我记录了一个时间显示,我认为的麻烦在于,由于 fps 会有所不同,因此并非在所有运行中都显示相同的时间标记,那么获胜帧的时间并不总是被渲染,因此获胜帧会在下一次更新中稍稍显示后发生.我使用 c# 和 unity 以防万一,但我认为它主要与此无关.非常感谢任何线索,我已经解决这个问题一段时间了

Hi all, Im working on a racing game and I decided to add a replay system to allow "ghost car" too, initially I was recordng data in some events like key pressed but only recording that data in all frames I manage a smooth replay, well its still ok as file is not huge and replay works, but the trouble is there is always a slight variation in time, only like 0.1 seconds or 0.2 at the most, I have a list of keyframes and in each position I record a time to be shown, the trouble I think is that because fps vary then not in all runs the same time marks are shown then the winning frame's time is not always being rendered so the winning frame happens in next update slightly after it should be shown. Im using c# and unity just in case, but I think its independent to this mainly. Thanks a lot about any clue, I have been around this issue for some time now

推荐答案

听起来您正在逐帧重放,正如您所发现的,这要求您的帧以与记录.在游戏渲染循环中,不能保证会发生这种情况.

It sounds like you're doing frame-by-frame replay which, as you've discovered, requires your frames to play back with the same delay as the recording. In a game-render loop, that's not guaranteed to happen.

当您每帧记录汽车状态(位置、航向等)时,您还需要记录时间戳(在这种情况下,从比赛开始累积 Time.deltaTime 就足够了).

As you record the car states (position, heading, etc) per frame, you need to also record a timestamp (in this case, accumulating Time.deltaTime from race start should suffice).

回放时,找到当前时间戳并从记录的边界帧中插入(即Lerp)汽车的状态.

When you play it back, find the current timestamp and interpolate (ie, Lerp) the car's state from the recorded bounding frames.

帧插值提示:

class Snapshot {
    public float Timestamp;

    public Matrix4x4 Transform;  // As an example.  Put more data here.
}

private int PrevIndex = 0;

private List<Snapshot> Snapshots = (new List<Snapshot>()).OrderBy(m => m.Timestamp).ToList();

private float GetLerpFactor(float currentTimestamp) {

    if ( PrevIndex == Snapshots.Count - 1) 
        return 0; // Reached end of Snapshots

    while (currentTimestamp >= Snapshots[PrevIndex + 1].Timestamp)
        PrevIndex++; // move 'frame' forward

    var currentDelta = Mathf.Max(0f, currentTimestamp - Snapshots[PrevIndex].Timestamp);
    var fullDelta = Snapshots[PrevIndex + 1].Timestamp - Snapshots[PrevIndex].Timestamp;

    var lerpFactor = currentDelta / fullDelta;
    return lerpFactor;
}

这篇关于解决不准确重播系统的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆