【问题标题】:Unity - Pass updating vector3 over GetComponent to move an objectUnity - 通过 GetComponent 传递更新 vector3 以移动对象
【发布时间】: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 格式

感谢您的回答。

【问题讨论】:

标签: c# unity3d transform


【解决方案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);
    }
 }
}

不用担心性能,因为高一级也不是内存,这绝对算不了什么。

编辑:进一步解释原因:

假设你有一张纸 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. Tadaaaa 上。它工作正常。如您所见,它有点慢,但不会影响您的性能。这是我告诉使用的情况。

 Vector3 currentPosition = scriptTransform.position; 

scriptTransform 是纸张 O 位置是桌子上的纸张 A。 currentPosition 是纸B。 W 是 OtherPlayer 脚本。

【讨论】:

  • 谢谢,这真的很有帮助,肯定是答案。我对 Vector3 vec = otherPlayer.position 的位置和原因感到困惑是必须的。我现在正在处理剩下的事情。这是获取对象的初始位置还是每个新位置并将其放在哪里? :S 谢谢!
  • 抱歉回复晚了,感谢您的补充说明。我在你的帮助下完成了这项工作!
【解决方案2】:

您的 OtherPlayer 脚本似乎还不错:每当您编辑 startPosition 时,它都会平滑地将游戏对象移动到某个位置(尽管您没有以非常容易理解的方式命名您的变量:

startPosition 应该类似于 newTargetPosition

currentPosition 应该类似于 targetPosition

transform.Position 始终是对象的确切位置(以防您不清楚)

我认为您应该检查的内容:(参见 cmets)

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;
}

【讨论】:

    猜你喜欢
    • 2019-03-07
    • 2017-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-26
    • 1970-01-01
    • 1970-01-01
    • 2013-03-28
    相关资源
    最近更新 更多