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格式
謝謝你的回答。
我發現這是一個很好的複習http://docs.unity3d.com/Manual/UnderstandingVectorArithmetic.html – yes
謝謝。這很方便,因爲我將移動到三個或更多矢量的平均值,而不是更遠處的單個矢量。 – elPato