This is a follow-up question to How to reapply force applied to one object to another object
I want to make two objects move, react to collisions and other stuff as if they were one. For example, Objects A and B are entangled. When Object C rams into Object A and starts moving with it, Object B should start moving with it. When Object B hits a wall, Object A should act like it also hit a wall. I tried changing position/speed every frame, but this only works when done in one direction. Now, I want all forces applied to any of the objects to also apply to the other objects entangled to it.
Edit: This is the new code which only fails when multiple collisions happen at once
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class EEScript : MonoBehaviour
{
public List<EEScript> activeObjects;
public new Rigidbody rigidbody;
public Vector3 lastPosition;
// Start is called before the first frame update
void Start()
{
rigidbody = gameObject.GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
lastPosition = transform.position;
}
private void OnCollisionEnter(Collision collision)
{
foreach (EEScript entangled in activeObjects)
{
if (entangled == this) continue;
Rigidbody rb = entangled.rigidbody;
if (rb is null) continue;
rb.velocity = rigidbody.velocity;
rb.angularVelocity = rigidbody.angularVelocity;
entangled.transform.position = entangled.lastPosition + transform.position - lastPosition;
}
}
}