Author Topic: Possible Bug in LagPosition Script  (Read 2136 times)

Sarper

  • Newbie
  • *
  • Thank You
  • -Given: 5
  • -Receive: 0
  • Posts: 21
    • View Profile
Possible Bug in LagPosition Script
« on: November 24, 2014, 09:54:52 AM »
There is possibly something very simple going on and I've probably fixed it with an overkill solution but here it goes.

Setup:

On my player prefab, the renderer and the collider are on two separate game objects. Renderer has the LagPosition script attached. Works great, no problems regarding jittering / stuttering whatsoever.

What happens:

Player1 enters the game.
Player1 moves it's avatar.
Player2 joins the game.
In Player2's instance, Player1's avatar smoothly moves from it's spawn point to it's current position instead of appearing in it's current position.

How I fixed it

I delayed the initialization of the LagPosition script. I set the mTrans, mAbsolute and mRelative variables a couple frames later so they get the synced position of the other avatars.

So is this a bug or am I missing something? At any rate, it's working now.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Possible Bug in LagPosition Script
« Reply #1 on: November 25, 2014, 02:06:47 AM »
The idea is that you should set OnRepositionEnd() in your player's Start() function, or whenever you know it finished initializing. However you can also probably achieve the same result by changing LagPosition to this:
  1. using UnityEngine;
  2.  
  3. /// <summary>
  4. /// Attach to a game object to make its position always lag behind its parent as the parent moves.
  5. /// </summary>
  6.  
  7. public class LagPosition : MonoBehaviour
  8. {
  9.         public Vector3 speed = new Vector3(10f, 10f, 10f);
  10.         public bool ignoreTimeScale = false;
  11.  
  12.         Transform mTrans;
  13.         Vector3 mRelative;
  14.         Vector3 mAbsolute;
  15.         bool mStarted = false;
  16.  
  17.         public void OnRepositionEnd ()
  18.         {
  19.                 Interpolate(1000f);
  20.         }
  21.  
  22.         void Interpolate (float delta)
  23.         {
  24.                 Transform parent = mTrans.parent;
  25.  
  26.                 if (parent != null)
  27.                 {
  28.                         Vector3 target = parent.position + parent.rotation * mRelative;
  29.                         mAbsolute.x = Mathf.Lerp(mAbsolute.x, target.x, Mathf.Clamp01(delta * speed.x));
  30.                         mAbsolute.y = Mathf.Lerp(mAbsolute.y, target.y, Mathf.Clamp01(delta * speed.y));
  31.                         mAbsolute.z = Mathf.Lerp(mAbsolute.z, target.z, Mathf.Clamp01(delta * speed.z));
  32.                         mTrans.position = mAbsolute;
  33.                 }
  34.         }
  35.  
  36.         void Awake () { mTrans = transform; }
  37.         void OnEnable () { if (mStarted) ResetPosition(); }
  38.         void Start () { mStarted = true; ResetPosition(); }
  39.  
  40.         public void ResetPosition ()
  41.         {
  42.                 mAbsolute = mTrans.position;
  43.                 mRelative = mTrans.localPosition;
  44.         }
  45.  
  46.         void Update ()
  47.         {
  48.                 Interpolate(ignoreTimeScale ? RealTime.deltaTime : Time.deltaTime);
  49.         }
  50. }