Created
February 11, 2018 07:23
-
-
Save neon-izm/ceea30f179879831c6a9279692554164 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEngine.Networking; | |
/// <summary> | |
/// UnetのNetworkTransformは位置が動かないと回転を同期してくれないという | |
/// めちゃくちゃつらいバグを抱えているため | |
/// networkTransformを取り付けたコンポーネントに、併せてこのスクリプトをアタッチしてください | |
/// </summary> | |
[RequireComponent(typeof(NetworkTransform))] | |
public class RotationFix : MonoBehaviour | |
{ | |
Vector3 oldPostion = Vector3.zero; | |
Quaternion oldRotation = Quaternion.identity; | |
[SerializeField] | |
[Tooltip("少数のクライアントだったら、無理矢理速く同期させちゃおう…")] | |
float sendInterval; | |
void Start() | |
{ | |
if (sendInterval < 0.016) | |
{ | |
sendInterval = Time.deltaTime; | |
} | |
GetComponent<NetworkTransform>().sendInterval = sendInterval; | |
} | |
void Update () | |
{ | |
if (!NetworkClient.active) | |
return; | |
if (transform.position != oldPostion) | |
{ | |
oldPostion = transform.position; | |
return; | |
} | |
var newRotation = transform.rotation; | |
//うう、計算コストが地味に大きい… | |
var diff = Quaternion.Angle(newRotation, oldRotation); | |
if (diff > 0.1f) | |
{ | |
// 姿勢が変わった | |
oldRotation = newRotation; | |
//ここがキモです。姿勢の変化だけだとNetworkTransformが同期をサボるので | |
// networkTransformに set dirtyフラグを付けてあげます…つっら | |
transform.position += new Vector3(0,0,0.00001f); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment