Fix: Adding comments on ingredients parenting to make it clear what is being done [MTT-8377] (#165)

* Fix: Adding comments on the rigidbody changes when parenting to be clear in what is being done [MTT-8377]
This commit is contained in:
Bastien Humeau 2024-04-25 14:12:55 -04:00 коммит произвёл GitHub
Родитель 2c96d6226e
Коммит 91a0a0778d
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
1 изменённых файлов: 10 добавлений и 6 удалений

Просмотреть файл

@ -1,4 +1,3 @@
using System;
using Unity.Netcode;
using Unity.Netcode.Components;
using UnityEngine;
@ -14,13 +13,18 @@ public class ServerIngredientPhysics : NetworkBehaviour
public override void OnNetworkObjectParentChanged(NetworkObject parentNetworkObject)
{
SetPhysics(parentNetworkObject == null);
// The ingredient's physic needs to be disabled when the ingredient is parented so that it's following the parent instead of moving freely.
SetKinematic(parentNetworkObject != null);
}
void SetPhysics(bool isEnabled)
private void SetKinematic(bool isEnabled)
{
m_Rigidbody.isKinematic = !isEnabled;
m_Rigidbody.interpolation = isEnabled ? RigidbodyInterpolation.Interpolate : RigidbodyInterpolation.None;
m_NetworkTransform.InLocalSpace = !isEnabled;
// Setting isKinematic to true will prevent the object from being affected by the physic forces in the game.
m_Rigidbody.isKinematic = isEnabled;
// When parented, rigidbody children are only moving because they receive their parent forces along with their own physic.
// When kinematic is true, they will ignore their parent forces and stay at the same world place.
// Changing the interpolation to None in that case will allow the object to keep the same local position instead of the world position.
m_Rigidbody.interpolation = isEnabled ? RigidbodyInterpolation.None : RigidbodyInterpolation.Interpolate;
m_NetworkTransform.InLocalSpace = isEnabled;
}
}