使用此代码生成:
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);
}
}