пример диаграммы
Я перемещаю свои твердые тела с помощью движущейся платформы следующим образом:
Код: Выделить всё
List linkedRbs = new List();
List linkedRbsRelativePos = new List();
private void FixedUpdate()
{
for (int i = 0; i < linkedRbs.Count; i++) // "linkedRbs" are the rbs touching the moving object
{
if (GetRelativePos(linkedRbs[i]) != linkedRbsRelativePos[i]) // RB is not in the same place as it was the last physics update
{
Vector3 lastRBPos = linkedRbsRelativePos[i];
linkedRbsRelativePos[i] = GetRelativePos(linkedRbs[i]);
Vector3 worldRelativePos = transform.position - linkedRbsRelativePos[i];
Vector3 newPos = lastRBPos + worldRelativePos;
//Debug.Log($"Dir={worldRelativePos},lastPos={lastRBPos},newerPos={newer}");
linkedRbs[i].MovePosition(new Vector3(newPos.x, linkedRbs[i].position.y, newPos.z));
}
}
}
private Vector3 GetRelativePos(Rigidbody rb)
{
Vector3 playerRelative = rb.transform.InverseTransformPoint(transform.position);
return playerRelative;
}
private void OnCollisionEnter(Collision collision)
{
Collider collider = collision.collider;
if(collider.TryGetComponent
(out PlayerColliderReference pcr))
{
Rigidbody _rb = pcr.GetComponentInParent();
linkedRbs.Add(_rb);
linkedRbsRelativePos.Add(GetRelativePos(_rb));
}
}
private void OnCollisionExit(Collision collision)
{
Collider collider = collision.collider;
if (collider.TryGetComponent(out PlayerColliderReference pcr))
{
Rigidbody _rb = pcr.GetComponentInParent();
for (int i = 0; i < linkedRbs.Count; i++)
{
if (linkedRbs[i] == _rb)
{
linkedRbs.Remove(linkedRbs[i]);
linkedRbsRelativePos.Remove(linkedRbsRelativePos[i]);
}
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/790 ... her-moving