1

使用此代码生成:

GameObject goX = Resources.Load ("Apple") as GameObject; GameObject goY = Resources.Load ("Orange") as GameObject; GameObject goZ = Resources.Load ("Banana") as 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);
}

}
4

1 回答 1

2

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

void Start () {
    myTransform = GetComponent<Transform> (); 
    syncPos = GetComponent<Transform>().position;
}
于 2016-03-14T07:23:54.150 回答