2017-04-13 73 views
1

最近,我正在研究一个滚球游戏(仅用于教育目的)。主要是我正在研究重播脚本。这意味着只要我点击回放按钮,玩家以前的移动应该是已知的。它应该和波斯王子的倒带功能相似。但我做错了什么。Unity我的重播脚本不工作?

using UnityEngine; 
using UnityStandardAssets.CrossPlatformInput; 

public class Player : MonoBehaviour { 
    private const int buffersize = 1000; 
    private MyKeyFrame[] playerskeyframe = new MyKeyFrame [buffersize]; 
    private Rigidbody rigidbody; 

    void Start() { 
     rigidbody = GetComponent<Rigidbody>(); 
    } 


    void Update() { 
     if (GameManager.Recording == true) { 
      Record(); 
     } else { 
      PlayBack(); 
     } 

    } 

    void PlayBack(){ 
     rigidbody.isKinematic = true; 
     int frame = Time.frameCount % buffersize; 
     this.transform.position = playerskeyframe [frame].Position; 
     this.transform.rotation = playerskeyframe [frame].rotation; 
    } 

    void Record(){ 
     Debug.Log (Time.frameCount % buffersize); 
     rigidbody.isKinematic = false; 
     int frame = Time.frameCount % buffersize; 
     float time = Time.time; 
     playerskeyframe [frame] = new MyKeyFrame (time, this.transform.position, this.transform.rotation); 
    } 
} 

public struct MyKeyFrame { 
    public float frametime; 
    public Vector3 Position; 
    public Quaternion rotation; 

    public MyKeyFrame (float atime,Vector3 apos, Quaternion arotation){ 
     frametime = atime; 
     Position = apos; 
     rotation = arotation; 
    } 

} 

编辑: - 什么不能在我的脚本。除非帧到达缓冲区大小为1000,否则播放功能无法按预期工作。这意味着如果没有完成一个循环,它就不会跳转到从记录中回放。 谢谢你的时间和考虑。

+0

究竟你的意思由_“但我做了什么错误。”_?什么对您的脚本不起作用? – Kardux

+0

我认为,直到您到达1000帧,数组中还有一些条目尚未设置,因此无法用于分配转换。 – DrRiisTab

回答

1

我建议切换到队列数据类型。只需添加您录制每帧的关键帧。 当您使用帧数的百分比时,某些东西会对阵列产生扭曲。你可能会像这样覆盖好的有效数据。

在记录功能

keyFrameQueue.Enqueue(new MyKeyFrame 
(
    time, this.transform.position, this.transform.rotation 
); 

并在回放只是把这个在更新发生的每一帧:

if(keyFrameQueue.Count > 0) 
{ 
    MyKeyFrame = keyFrameQueue.Dequeue(); 
    this.transform.position = MyKeyFrame.Position; 
    this.transform.rotation = MyKeyFrame.rotation; 
}