Ricardo Lopes 4ec5a39aa6
WIP: Sync ship position (#22)
* Add TransformSync behaviour

* NetworkPlayer using TransformSync

* TransformSync adjustments to make it work for more than just player objects

* Rename for clarity

* Network ship being created from prefab

* Use isAuthority instead of isLocalPlayer

* Actually sync ships

* Fix offset

* Perfect offset

* Smooth damp position

* Smooth damn transform rotation

* Showing ship landing gear

* Sync ships with sectors

* Source comment for quaternion smooth damp

* just some small tweaks (#25)

Co-authored-by: AmazingAlek <alek.ntnu@gmail.com>
2020-02-21 21:51:58 +01:00

34 lines
1.3 KiB
C#

using UnityEngine;
namespace QSB
{
static class Helpers
{
// Stolen from here: https://gist.github.com/maxattack/4c7b4de00f5c1b95a33b
public static Quaternion QuaternionSmoothDamp(Quaternion rot, Quaternion target, ref Quaternion deriv, float time)
{
// account for double-cover
var Dot = Quaternion.Dot(rot, target);
var Multi = Dot > 0f ? 1f : -1f;
target.x *= Multi;
target.y *= Multi;
target.z *= Multi;
target.w *= Multi;
// smooth damp (nlerp approx)
var Result = new Vector4(
Mathf.SmoothDamp(rot.x, target.x, ref deriv.x, time),
Mathf.SmoothDamp(rot.y, target.y, ref deriv.y, time),
Mathf.SmoothDamp(rot.z, target.z, ref deriv.z, time),
Mathf.SmoothDamp(rot.w, target.w, ref deriv.w, time)
).normalized;
// compute deriv
var dtInv = 1f / Time.deltaTime;
deriv.x = (Result.x - rot.x) * dtInv;
deriv.y = (Result.y - rot.y) * dtInv;
deriv.z = (Result.z - rot.z) * dtInv;
deriv.w = (Result.w - rot.w) * dtInv;
return new Quaternion(Result.x, Result.y, Result.z, Result.w);
}
}
}