如何通过一系列四元素旋转游戏对象



im试图通过我通过CSV文件读取的一系列四元素来旋转游戏对象。目前,它没有旋转对象,我相信我没有正确更新转换。对此进行任何帮助,将不胜感激,让我知道您是否需要更多信息。

rotationAnimator.cs 旋转器脚本:

public class RotationAnimator : MonoBehaviour
{
    public Transform playbackTarget;
    [HideInInspector]
    public bool playingBack = false;
    public CSVReader csvReader;
    public void PlayRecording()
    {
        //  -1 as it reads the last blank line of the CSV for reasons
        for (int row = 0; row < csvReader.quaternionRecords.Length-1; row++)
        {
            playbackTarget.rotation = new Quaternion(csvReader.quaternionRecords[row].x, csvReader.quaternionRecords[row].y, csvReader.quaternionRecords[row].z, csvReader.quaternionRecords[row].w);
        }
    }
}

csvreader.cs 脚本读取CSV文件

public class CSVReader : MonoBehaviour
{
    public QuaternionRecord[] quaternionRecords;
    public List<List<String>> fileData;
    ILogging logger;
    string path = "Assets\Logs\RotationList.csv";
    private void OnEnable()
    {
        logger = Logging.GetCurrentClassLogger();
    }
    public void ReadFile()
    {
        var r = File.OpenText(path);
        fileData = r.ReadToEnd().Split('n').Select(s => s.Split(',').ToList()).ToList();
        quaternionRecords = new QuaternionRecord[fileData.Count];
        // skip last empty line at "file data.count -1"
        for(int row = 0; row <  fileData.Count-1; row++)
        {
            try
            {
                quaternionRecords[row] = new QuaternionRecord();
                quaternionRecords[row].time = float.Parse(fileData[row][0]);
                quaternionRecords[row].x = float.Parse(fileData[row][1]);
                quaternionRecords[row].y = float.Parse(fileData[row][2]);
                quaternionRecords[row].z = float.Parse(fileData[row][3]);
                quaternionRecords[row].w = float.Parse(fileData[row][4]);
            }
            catch(Exception e)
            {
                Debug.Log("Ouch!");
            }
        }
        r.Close();
    }
    public struct QuaternionRecord
    {
        public float time;
        public float x;
        public float y;
        public float z;
        public float w;
    }
}

playRecorting被称为:

    public void Playback()
    {
        Debug.Log("Beginning Playback...");
        csvReader.ReadFile();
        rotationAnimator.PlayRecording();
    }

这是一种通过修改现有代码来使用Coroutines进行quaternionRecords的方法。

//Change void to IEnumerator 
public IEnumerator PlayRecording()
{
    for (int row = 0; row < csvReader.quaternionRecords.Length; row++)
    {
        playbackTarget.rotation = new Quaternion(csvReader.quaternionRecords[row].x, csvReader.quaternionRecords[row].y, csvReader.quaternionRecords[row].z, csvReader.quaternionRecords[row].w);
        //Add this line to wait 1 second until next itteration
        yield return new WaitForSeconds(1f);
    }
}
public void Playback()
{
    Debug.Log("Beginning Playback...");
    csvReader.ReadFile();
    //Change to StartCourotine
    StartCoroutine(rotationAnimator.PlayRecording());
}

我尚未测试代码,因此它可能不起作用,它可能不是最好的解决方案,但这是一种方法。其他方法是将UpdateTime.deltaTime

一起使用

最新更新