Friction does what I want for sliding, but I was wondering if there is something to prevent two colliders from getting separated as well.
I want to make an “adhesive harpoon” that can release with a given keypress. Is there any way to get that effect while still keeping the objects separated?
Hello, This could be achieved in a number of ways. The easiest would be to set one of the objects to be a child of the other, that way they will move together as though they are one object, but if you want to avoid parenting objects for whatever reason you could also try creating a fixed Joint (or spring joint if you want some elasticity) and the point of contact.
The following code snippet is more a guide, I wrote it in here and have not tested it so you will probably need to modify it
void OnCollisionEnter(Collision col)
{
// creates joint
FixedJoint joint = gameObject.AddComponent<FixedJoint>();
// sets joint position to point of contact
joint.anchor = col.contacts[0].point;
// conects the joint to the other object
joint.connectedBody = col.contacts[0].otherCollider.transform.GetComponentInParent<Rigidbody>();
// Stops objects from continuing to collide and creating more joints
joint.enableCollision = false;
}
I hope this helps
I dont see why this wouldnt work.
RigidBody body;
Transform collidedObject;
void OnCollisionEnter(Collision collision)
{
colliedObject = collison.transform;
if(!body)
{
if(collision.gameobject.GetComponent<RigidBody>())
{
body = collision.gameobject.GetComponent<RigidBody>();
}
}
if(body)
body.isKinematic = true;
this.gameobject.transform.setparent(collision.gameobject.transform);
}
Void Update()
{
if(Input.GetKeyDown(KeyCode.space)
{
if(collidedObject)
{
collidedObject.parent = null;
if(body)
{
body.isKinematic = false;
body = null;
}
}
}
}
Have you considered adding a “Adhesive Point” to each. Like a component on the object. It Two plane, and each has a force of some huge magnitude pressing into the other. Because they’re planes, theyll flatten against eachother. Friction then keeps it from sliding