[英]Unity - Pass updating vector3 over GetComponent to move an object
Unity3D版本4.5.3f3 Pro。
我是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 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);
}
}
}
不必担心性能,因为它比内存高一个级别,所以绝对不算什么。
编辑:进一步解释为什么:
假设您有一张纸A(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上塔达阿 工作正常。 如您所见,它速度稍慢,但不会影响性能。 我告诉您使用这种情况。
Vector3 currentPosition = scriptTransform.position;
scriptTransform是纸张O的位置是桌子上的纸张A。 currentPosition是paperB。 W是OtherPlayer脚本。
您的OtherPlayer脚本似乎还不错:每当您编辑startPosition时,它将平滑地将gameObject移至某个位置(尽管您没有以一种非常容易理解的方式命名变量:
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.