Unity-Technologies / com.unity.netcode.gameobjects

Netcode for GameObjects is a high-level netcode SDK that provides networking capabilities to GameObject/MonoBehaviour workflows within Unity and sits on top of underlying transport layer.
MIT License
2.1k stars 430 forks source link

World coordinate NetworkTransform.Teleport that internally handles InLocalSpace #2856

Open zachstronaut opened 3 months ago

zachstronaut commented 3 months ago

I'm using an extension method right now:

public static void TeleportInWorld( this NetworkTransform _ref, Vector3 _worldPosition, Quaternion _worldRotation )
{
    Debug.Assert(_ref.CanCommitToTransform);
    bool needsChange = _ref.InLocalSpace && _ref.transform.parent;
    var newPos = needsChange ? _ref.transform.parent.InverseTransformPoint(_worldPosition) : _worldPosition;
    var newRot = needsChange ? _ref.transform.parent.rotation.Inverted() * _worldRotation : _worldRotation;
    _ref.Teleport(newPos, newRot, _ref.transform.localScale);
}
zachstronaut commented 3 months ago

Feel like you could certain do it this way too?

public static void TeleportInWorld( this NetworkTransform _ref, Vector3 _worldPosition, Quaternion _worldRotation )
{
    Debug.Assert(_ref.CanCommitToTransform);
    _ref.transform.position = _worldPosition;
    _ref.transform.rotation = _worldRotation;
    var newPos = _ref.InLocalSpace ? _ref.transform.localPosition : _ref.transform.position;
    var newRot = _ref.InLocalSpace ? _ref.transform.localRotation : _ref.transform.rotation;
    _ref.Teleport(newPos, newRot, _ref.transform.localScale);
}