How to create a fixed joint when two objects collide?
Hello, for a project I am working on (Unity 2020.1.5f1 for Oculus Quest) I need to have two objects that initially move independently of one another, but move together once they collide. My project was initially using VRTK v3, which has a feature that is supposed to do what I want it to do; however, the VRTK v3 package is completely non-functional on my computer (even when the files are present in the project) and the files self-delete whenever the Unity editor is restarted, so I am using Unity's XR Interaction Toolkit instead for the interactions I need (which does NOT come with the feature I need to make this). I am trying to write a script that creates a fixed joint on collision, but so far, nothing works. It seems that the game is not registering when the two objects collide. This is the latest script I have attempted:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SnapScript : MonoBehaviour
{
private void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.GetComponent<Rigidbody>()!=null)
{
gameObject.AddComponent<FixedJoint>();
gameObject.GetComponent<FixedJoint>().connectedBody = collision.rigidbody;
}
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
Any help getting the desired behavior would be appreciated; I've so far gotten no help and nothing I've found seems to get this working. Thanks.