Skip to content

Instantly share code, notes, and snippets.

@d12
Last active March 11, 2021 13:53
Show Gist options
  • Save d12/43e4b4425141bf3c7d24820f20e9563e to your computer and use it in GitHub Desktop.
Save d12/43e4b4425141bf3c7d24820f20e9563e to your computer and use it in GitHub Desktop.
Fixes issues caused by RealtimeTransform's sometimes dropping important updates
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// This class is needed because RealtimeTransforms use unreliable properties, which means sometimes
// packets are dropped. This normally isn't a big deal, unless your app relies on a single
// programmatic position update. If this single update is dropped, the position of the RealtimeTransform
// will appear very incorrect to other users, but correct to the RealtimeTransform owner.
//
// So to fix this, after a large update we enable the Jiggle script which will jiggle the object back and forth
// a few times by a tiny unnoticable amount. As long as one of these jiggle packets are sent properly,
// clients will see a correct object position.
public class Jiggle : MonoBehaviour
{
private int _jigglesRemaining = 0;
public void Jiggle()
{
_jigglesRemaining = 6;
enabled = true;
}
void FixedUpdate()
{
if (_jigglesRemaining > 0)
{
Vector3 pos = transform.position;
if (_jigglesRemaining % 2 == 0)
{
pos.x += 0.00001f;
}
else
{
pos.x -= 0.00001f;
}
transform.position = pos;
_jigglesRemaining -= 1;
}
else
{
enabled = false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment