older velnet changes - seems to be mostly samples

upm
Anton Franzluebbers 2022-08-15 12:53:49 -04:00
parent 6d52a338c5
commit 78076c7f41
2 changed files with 67 additions and 31 deletions

View File

@ -9,12 +9,18 @@ namespace VelNet
[AddComponentMenu("VelNet/VelNet Sync Transform")] [AddComponentMenu("VelNet/VelNet Sync Transform")]
public class SyncTransform : NetworkSerializedObjectStream public class SyncTransform : NetworkSerializedObjectStream
{ {
public bool useLocalTransform; [Space]
[Tooltip("0 to disable.")] public bool position = true;
public float teleportDistance; public bool rotation = true;
[Tooltip("0 to disable.")] [Tooltip("Scale is always local")] public bool scale;
public float teleportAngle;
[Space]
public bool useLocalTransform;
[Tooltip("0 to disable.")] public float teleportDistance;
[Tooltip("0 to disable.")] public float teleportAngle;
private Vector3 targetScale;
private Vector3 targetPosition; private Vector3 targetPosition;
private Quaternion targetRotation; private Quaternion targetRotation;
private float distanceAtReceiveTime; private float distanceAtReceiveTime;
@ -32,6 +38,8 @@ namespace VelNet
targetPosition = transform.position; targetPosition = transform.position;
targetRotation = transform.rotation; targetRotation = transform.rotation;
} }
targetScale = transform.localScale;
} }
/// <summary> /// <summary>
@ -39,8 +47,9 @@ namespace VelNet
/// </summary> /// </summary>
protected override void SendState(BinaryWriter writer) protected override void SendState(BinaryWriter writer)
{ {
writer.Write(transform.localPosition); if (position) writer.Write(transform.localPosition);
writer.Write(transform.localRotation); if (rotation) writer.Write(transform.localRotation);
if (scale) writer.Write(transform.localScale);
} }
/// <summary> /// <summary>
@ -49,8 +58,9 @@ namespace VelNet
/// </summary> /// </summary>
protected override void ReceiveState(BinaryReader reader) protected override void ReceiveState(BinaryReader reader)
{ {
targetPosition = reader.ReadVector3(); if (position) targetPosition = reader.ReadVector3();
targetRotation = reader.ReadQuaternion(); if (rotation) targetRotation = reader.ReadQuaternion();
if (scale) targetScale = reader.ReadVector3();
// record the distance from the target for interpolation // record the distance from the target for interpolation
if (useLocalTransform) if (useLocalTransform)
@ -61,6 +71,7 @@ namespace VelNet
{ {
transform.localPosition = targetPosition; transform.localPosition = targetPosition;
} }
if (teleportAngle != 0 && teleportAngle < angleAtReceiveTime) if (teleportAngle != 0 && teleportAngle < angleAtReceiveTime)
{ {
transform.localRotation = targetRotation; transform.localRotation = targetRotation;
@ -74,6 +85,7 @@ namespace VelNet
{ {
transform.position = targetPosition; transform.position = targetPosition;
} }
if (teleportAngle != 0 && teleportAngle < angleAtReceiveTime) if (teleportAngle != 0 && teleportAngle < angleAtReceiveTime)
{ {
transform.rotation = targetRotation; transform.rotation = targetRotation;
@ -87,28 +99,52 @@ namespace VelNet
if (useLocalTransform) if (useLocalTransform)
{ {
transform.localPosition = Vector3.MoveTowards( if (position)
transform.localPosition, {
targetPosition, transform.localPosition = Vector3.MoveTowards(
Time.deltaTime * distanceAtReceiveTime * serializationRateHz transform.localPosition,
); targetPosition,
transform.localRotation = Quaternion.RotateTowards( Time.deltaTime * distanceAtReceiveTime * serializationRateHz
transform.localRotation, );
targetRotation, }
Time.deltaTime * angleAtReceiveTime * serializationRateHz
); if (rotation)
{
transform.localRotation = Quaternion.RotateTowards(
transform.localRotation,
targetRotation,
Time.deltaTime * angleAtReceiveTime * serializationRateHz
);
}
} }
else else
{ {
transform.position = Vector3.MoveTowards( if (position)
transform.position, {
targetPosition, transform.position = Vector3.MoveTowards(
Time.deltaTime * distanceAtReceiveTime * serializationRateHz transform.position,
); targetPosition,
transform.rotation = Quaternion.RotateTowards( Time.deltaTime * distanceAtReceiveTime * serializationRateHz
transform.rotation, );
targetRotation, }
Time.deltaTime * angleAtReceiveTime * serializationRateHz
if (rotation)
{
transform.rotation = Quaternion.RotateTowards(
transform.rotation,
targetRotation,
Time.deltaTime * angleAtReceiveTime * serializationRateHz
);
}
}
if (scale)
{
transform.localScale = Vector3.Lerp(
transform.localScale,
targetScale,
Time.deltaTime * serializationRateHz
); );
} }
} }

View File

@ -1206,7 +1206,7 @@ namespace VelNet
NetworkObject prefab = instance.prefabs.Find(p => p.name == prefabName); NetworkObject prefab = instance.prefabs.Find(p => p.name == prefabName);
if (prefab == null) if (prefab == null)
{ {
VelNetLogger.Error("Couldn't find a prefab with that name: " + prefabName); VelNetLogger.Error("Couldn't find a prefab with that name: " + prefabName + "\nMake sure to add the prefab to list of prefabs in VelNetManager");
return null; return null;
} }