为什么对象总是将自己定位在@ 0.0而不是它最初产生的位置

时间:2016-03-13 18:39:15

标签: unity3d multiplayer unity3d-unet

使用此代码生成:

GameObject goX = Resources.Load(" Apple")作为GameObject;         GameObject goY = Resources.Load(" Orange")作为GameObject;         GameObject goZ = Resources.Load(" Banana")作为GameObject;

    GameObject go1 = GameObject.Instantiate (goX, Spawn3.transform.position, Quaternion.identity) as GameObject; 
    NetworkServer.Spawn (go1);
    GameObject go2 = GameObject.Instantiate (goY, Spawn4.transform.position, Quaternion.identity) as GameObject; 
    NetworkServer.Spawn (go2);
    GameObject go3 = GameObject.Instantiate (goZ, Spawn1.transform.position, Quaternion.identity) as GameObject; 
    NetworkServer.Spawn (go3);

Spawn1 - Spawn4不是0.0

当对象产生时,它会在正确的位置上产生,但然后立即移动到0.0位置。

这是我在对象上使用的同步脚本,我认为问题出现在脚本中,因为当我使用它运行的NetworkTransform组件时。但是,NetworkTransform错过了一些动作,所以我想在脚本中同步:

using UnityEngine;
using System.Collections;
using UnityEngine.Networking;

public class Object_SyncPosition : NetworkBehaviour {

private Transform myTransform;
[SerializeField] float lerpRate = 15;
[SyncVar] private Vector3 syncPos;

void Start () {
    myTransform = GetComponent<Transform> ();
}

void Update () {
    if (hasAuthority) {
        TransmitPosition ();
    }

    LerpPosition ();

}

void LerpPosition () {
    if (!isLocalPlayer) {
        myTransform.position = Vector3.Lerp (myTransform.position, syncPos, Time.deltaTime * lerpRate);
    }
}

[Command]
void Cmd_ProvidePositionToServer (Vector3 pos) {
    syncPos = pos;
}

[ClientCallback]
void TransmitPosition () {
    Cmd_ProvidePositionToServer (myTransform.position);
}

}

1 个答案:

答案 0 :(得分:2)

我认为您的对象正朝着您的syncPos移动(默认情况下为0.0)。尝试将启动脚本中的syncPos变换设置为您的生成位置。

void Start () {
    myTransform = GetComponent<Transform> (); 
    syncPos = GetComponent<Transform>().position;
}