2017-02-16 1 views
0

임 CSV 파일을 통해 읽은 Quaternions의 배열을 통해 게임 객체를 회전하려고합니다. 그것의 현재 내가 아닙니다 transform.rotation을 올바르게 업데이트하지 않는다고 생각하는 objectas를 회전시키지 않습니다. 이 문제를 해결하는 데 큰 도움이 될 것입니다. 더 이상 정보가 필요하면 알려주세요.Quaternions 배열을 통해 게임 개체를 회전하는 방법은 무엇입니까?

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; 
    } 
} 
을 읽는 스크립트 PlayRecording이라고합니다

:

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

을 어떻게 사용하고 있습니까? PlayRecording을 어디에서 전화하고 있습니까? –

+0

다른 스크립트에서 함수는 다음과 같습니다. public void Playback() { Debug.Log ("Beginning Playback ..."); csvReader.ReadFile(); rotationAnimator.PlayRecording(); rotationAnimator.playingBack = true; } – Nilmag

+0

나는 당신이 무엇을보고 있는지, 나는 bool을 없애고 필요한 통화 기능을 추가했습니다. – Nilmag

답변

1

기존 코드를 수정하여 quaternionRecordsCoroutines을 사용하여 반복하는 방법입니다. 그것이 작동하지 않을 수 있습니다 그것은 최상의 솔루션되지 않을 수도 있습니다

//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

+1

기록 된 각 회전의 타임 스탬프를 저장하고 업데이트/고정 업데이트의 기록을 통해 현재 시간에 따라 현재 회전을 설정할 수 있습니다. 행을 대기열에 넣고 업데이트 대기열에서 꺼 냈습니다. –

관련 문제