【发布时间】:2016-05-17 12:57:27
【问题描述】:
Unity3D 版本 4.5.3f3 专业版。
我是 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.cs”的对象“otherPlayer”:
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);
}
}
}
最新Debug 'startPosition.ToString()'的结果:
Is there anybody out there??: (0.0, 0.0, 0.0)
UnityEngine.Debug:Log(Object)
- 如何正确传递这些值
- 如何确认它们是 vector3 格式
感谢您的回答。
【问题讨论】:
-
谢谢。这很方便,因为我将移动到一个点,该点是三个或更多向量的平均值,而不是更远的单个向量。