2017-02-16 1 views
0

En essayant de faire tourner un objet gameobject à travers un tableau de Quaternions que j'ai lu via un fichier CSV. Il ne tourne actuellement pas les objectas je crois que je ne suis pas en train de mettre à jour la transform.rotation correctement. Toute aide pour résoudre ce problème serait grandement appréciée, faites-moi savoir si vous avez besoin de plus d'informations.Comment faire pivoter un objet de jeu à travers un tableau de Quaternions?

RotationAnimator.cs rotateur 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 Le script lecture du fichier 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; 
    } 
} 

Où PlayRecording est appelé:

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

Comment et où appelez-vous PlayRecording? –

+0

Dans un autre script, voici la fonction: public void Lecture() { Debug.Log ("Beginning Playback ..."); csvReader.ReadFile(); rotationAnimator.PlayRecording(); rotationAnimator.playingBack = true; } – Nilmag

+0

Je vois ce que vous obtenez, je me suis débarrassé de la bool et ajouté la fonction d'appel nécessaire – Nilmag

Répondre

1

Voici un moyen de parcourir le quaternionRecords en utilisant Coroutines en modifiant votre code existant.

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

Je n'ai pas testé le code afin qu'il pourrait ne pas fonctionner et il pourrait ne pas être la meilleure solution, mais il est une façon de le faire. D'autres façons utilise Update avec Time.deltaTime

+1

Vous pouvez enregistrer un horodatage de chaque rotation enregistrée et parcourir les enregistrements en mise à jour/mise à jour fixe et définir la rotation actuelle en fonction de l'heure actuelle. Je mettrais des rangées à la file d'attente, et a pris le hors de la file d'attente dans la mise à jour. –