Search Unity

Why is the GameObject return to 0,0 position

Discussion in 'Multiplayer' started by pKallv, Jan 30, 2016.

  1. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,191
    When I use the NetworkTransform module attached to the GameObject all works well but when i use this script instead, the object returns to position 0,0, were it was spawned after drag.

    Here is the code i use (from a tutorial):

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4.  
    5. public class SyncPos : NetworkBehaviour {
    6.  
    7.     [SyncVar] private Vector3 syncPos;
    8.     Transform myTransform;
    9.     [SerializeField] float lerpRate = 15;
    10.  
    11.     void Start() {
    12.         myTransform = GetComponent<Transform> ();
    13.     }
    14.  
    15.     void FixedUpdate () {
    16.         TransmitPosition ();
    17.         LerpPosition ();
    18.     }
    19.  
    20.     void LerpPosition () {
    21.         if (!isLocalPlayer) {
    22.             myTransform.position = Vector3.Lerp (myTransform.position, syncPos, Time.deltaTime * lerpRate);
    23.         }
    24.     }
    25.  
    26.     [Command]
    27.     void CmdProvidePositionToServer (Vector3 pos) {
    28.         syncPos = pos;
    29.     }
    30.  
    31.     [ClientCallback]
    32.     void TransmitPosition () {
    33.         if (isLocalPlayer) {
    34.             CmdProvidePositionToServer (myTransform.position);
    35.         }
    36.     }
    37. }
    The code i use for drag:

    Code (CSharp):
    1. public void On_Drag (Gesture gesture) {
    2.  
    3.         if (!isLocalPlayer)
    4.             return;
    5.  
    6.         if (gesture.pickedObject.tag == "Background")
    7.             return;
    8.  
    9.         // Check if there have been a linecast, if so use that Game Object for drag
    10.         if (selected_GO != null) {
    11.             selected_GO.transform.position = Camera.main.ScreenToWorldPoint(new Vector3(gesture.position.x, gesture.position.y, 1f));
    12.         } else if (gesture.pickedObject != null) {
    13.             gesture.pickedObject.transform.position = Camera.main.ScreenToWorldPoint(new Vector3(gesture.position.x, gesture.position.y, 1f));
    14.         }
    15.  
    16.     }
     
  2. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    Code (csharp):
    1.  
    2.  myTransform.position=Vector3.Lerp(myTransform.position, syncPos, Time.deltaTime* lerpRate);
    3.  
    There's your problem. You need to store your elapsed time to a different variable, then pass the variable to Lerp(). Lerp() requires you need two fixed Vector points and 1 continuously updating variable in order to obtain a position between the two fixed Vector points.
     
  3. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,191
    Strange i tested the exact same code on an other GameObject and that worked?? Anyhow, thanks i will look into that.
     
  4. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,191
    I solved it by not using the SyncPos script but rather adding the network.transform component.