Nilmag
Nilmag

Reputation: 573

How to rotate a game object through an array of Quaternions?

Im trying to rotate a gameobject through an array of Quaternions that I've read in via a CSV file. Its currently not rotating the objectas i believe i'm not updating the transform.rotation correctly. Any help resolving this would be greatly appreciated, let me know if you need anymore information.

RotationAnimator.cs The Rotator Script:

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 The script reading the csv file

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;
    }
}

Where PlayRecording is called:

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

Upvotes: 0

Views: 286

Answers (1)

Johan Lindkvist
Johan Lindkvist

Reputation: 1784

Here is a way to itterate the quaternionRecords using Coroutines by modifying your existing code.

//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());
}

I haven't tested the code so it might not work and it might not be the best solution but it is a a way to do it. Other ways is using Update with Time.deltaTime

Upvotes: 1

Related Questions