2016-05-17 66 views
0

Unity3D版本4.5.3f3 Pro。Unity - 通過GetComponent更新vector3以移動對象

我是Unity和C#的新手,我很努力地理解爲什麼我的對象沒有更新(移動)到我試圖發送的新的Vector3值。

背景:我使用套接字將3個值的數組傳入統一。在執行數據的Debug.Log時,我收到的值很好。數據在每秒遞增的設定時間上更新(即,「位置」:「-10,10,10」秒後「位置」:「-11,11,11」等等)

我有掙扎瞭解Vector3,但已經想出了這個。

與腳本

空對象:

public void PlayerMove(SocketIOEvent e) 
{ 
    Debug.Log("e.data: " + e.data); 

    string newVectorString = e.data.ToString(); 
    Debug.Log("newVectorString: " + newVectorString); 

    string[] temp = newVectorString.Split(','); 
    float x = float.Parse(temp[0]); 
    float y = float.Parse(temp[1]); 
    float z = float.Parse(temp[2]); 

    Vector3 newPosition = new Vector3(x,y,z); 
    otherPlayer.GetComponent<OtherPlayer>().startPosition = newPosition; 
} 

兩個日誌(不斷更新)的結果是:

e.data: {"Position":"-19,19,0"} 
UnityEngine.Debug:Log(Object) 

newVectorString: {"Position":"-19,19,0"} 
UnityEngine.Debug:Log(Object) 

所以我接收數據耶!我已將它移入字符串'newVectorString'。然後,我嘗試通過分割字符串並將其通過GetComponent創建一個新向量,如下所示:

otherPlayer.GetComponent<OtherPlayer>().startPosition = newPosition; 

下一部分。

對象 「otherPlayer」 與腳本 'OtherPlayer.cs':

using UnityEngine; 
using System.Collections; 
using System.Collections.Generic; 

public class OtherPlayer : MonoBehaviour 
{ 
public Vector3 startPosition; 
public Vector3 currentPosition = Vector3.zero; 

void Start() 
{ 

} 

void Update() 
{ 

    Debug.Log("Is there anybody out there??: " + startPosition.ToString()); 

    if (startPosition != Vector3.zero) 
    { 
     currentPosition = startPosition; 
     startPosition = Vector3.zero; 
     transform.position = Vector3.Lerp (transform.position, currentPosition, 50 * Time.deltaTime); 
    } 
    else 
    { 
     transform.position = Vector3.Lerp (transform.position, currentPosition, 50 * Time.deltaTime); 
    } 
} 
} 

最新調試 'startPosition.ToString()' 的結果:

Is there anybody out there??: (0.0, 0.0, 0.0) 
UnityEngine.Debug:Log(Object) 
  • 哪有我正確地通過這些值
  • 我如何確認它們是vector3格式

謝謝你的回答。

+1

我發現這是一個很好的複習http://docs.unity3d.com/Manual/UnderstandingVectorArithmetic.html – yes

+0

謝謝。這很方便,因爲我將移動到三個或更多矢量的平均值,而不是更遠處的單個矢量。 – elPato

回答

1
Vector3 vec = otherPlayer.position; 

這會將位置的值複製到vec中。現在你有兩個獨立的重複。改變一個不會影響另一個。

您需要在OtherPlayer中保留對PlayerMove的引用並跟蹤位置的變化。

public class OtherPlayer : MonoBehaviour{ 
    private OtherScript script= null; 
    private void Start(){ 
     this.script= GetOtherScript(); // you should know how to get it 
    } 
    void Update() 
{ 

    Debug.Log("Is there anybody out there??: " + this.script.startPosition.ToString()); 

    if (startPosition != Vector3.zero) 
    { 
     currentPosition = this.script.startPosition; 
     startPosition = Vector3.zero; 
     transform.position = Vector3.Lerp (transform.position, currentPosition, 50 * Time.deltaTime); 
    } 
    else 
    { 
     transform.position = Vector3.Lerp (transform.position, currentPosition, 50 * Time.deltaTime); 
    } 
} 
} 

不要擔心表現,因爲高一級也不記憶,這是絕對沒有的。

編輯:爲了進一步解釋爲什麼:

考慮你有(10,10,10)紙張上的,你拿紙B和讀出的值,並將其寫入到文件B.現在A和B閱讀(10,10,10)。換紙A到(20,20,20),紙B沒有理由自動改變,所以仍然是(10,10,10)。這就是你在做什麼。

Vector3 currentPosition = newPosition; 

要解決該問題,請先將紙張A寫上(10,10,10),然後放在桌子上。拿一張紙O並寫上(桌子)。現在拿一張紙W寫上(O,B紙)。 W有一個方法將使用O來寫在B上。所以它讀取O,O告訴桌子,然後你去桌子上讀(10,10,10)並在B上寫下。一切都很好。

您將A更新爲(20,20,20),紙W再次運行該方法,它告訴Desk,O到桌面找到A並讀取(20,20,20)並寫入它結束B. Tadaaaa。它工作正常。正如你可以看到它有點慢,但不會影響你的表現。這是我告訴使用的情況。

Vector3 currentPosition = scriptTransform.position; 

scriptTransform是紙張O位置是桌子上的紙張A. currentPosition是paperB。 W是OtherPlayer腳本。

+0

謝謝這真的很有幫助,肯定會是答案。我很困惑,爲什麼Vector3 vec = otherPlayer.position;是必須的。我現在正在休息。這是否獲得了對象或每個新位置的初始位置,並將其放置在哪裏? :S謝謝! – elPato

+0

檢查編輯,這可能有所幫助。 – Everts

+0

對不起,延遲迴復,謝謝你的補充說明。我得到了你的幫助! – elPato

1

您OtherPlayer腳本似乎好:它會順利的遊戲物體移動到每當編輯指定startPosition(雖然你沒有一個很容易理解的方式命名變量一定的位置:

startPosition應該是這樣的newTargetPosition

currentPosition應該像targetPosition

transform.Position總是對象的確切位置(如果它是不明確的你)

我想你應該檢查:(見註釋)

public void PlayerMove(SocketIOEvent e) 
{ 
    Debug.Log("e.data: " + e.data); 

    string newVectorString = e.data.ToString(); 
    Debug.Log("newVectorString: " + newVectorString); 

    // CHECK THIS: 
    // temp[0] is equal to {"Position":"-19  --> Won't parse 
    string[] temp = newVectorString.Split(','); 
    float x = float.Parse(temp[0]); 
    float y = float.Parse(temp[1]); 
    float z = float.Parse(temp[2]); 

    Vector3 newPosition = new Vector3(x,y,z); 

    // Check this: print new position to see if it is correct 
    // Check if otherPlayer is the object you expect to have here 
    otherPlayer.GetComponent<OtherPlayer>().startPosition = newPosition; 
}