1
votes

Use this code to spawn:

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 is not 0.0

When the object is spawned it spawn on the correct position but then, immediately, moves to 0.0 position.

This is the sync script i use on the object, I think the problem is in the script as when i use the NetworkTransform component it works. However, the NetworkTransform misses some moves so i want to sync in script:

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

1 Answers

2
votes

I think your object is moving towards your syncPos (which by default is 0.0). Try to set syncPos transform in the start script to your spawn location.

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