为什么对象总是将自己定位为@ 0.0 而不是最初生成的对象
Why is the object always position itself @ 0.0 and not were it is originally spawned
使用此代码生成:
GameObject goX = Resources.Load ("Apple") 作为游戏对象;
GameObject goY = Resources.Load ("Orange") 作为游戏对象;
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);
}
}
我认为您的对象正在向您的 syncPos 移动(默认情况下为 0.0)。尝试在启动脚本中将 syncPos 转换设置为您的重生位置。
void Start () {
myTransform = GetComponent<Transform> ();
syncPos = GetComponent<Transform>().position;
}
使用此代码生成:
GameObject goX = Resources.Load ("Apple") 作为游戏对象; GameObject goY = Resources.Load ("Orange") 作为游戏对象; 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);
}
}
我认为您的对象正在向您的 syncPos 移动(默认情况下为 0.0)。尝试在启动脚本中将 syncPos 转换设置为您的重生位置。
void Start () {
myTransform = GetComponent<Transform> ();
syncPos = GetComponent<Transform>().position;
}