Skip to content

Instantly share code, notes, and snippets.

@N-Carter
Created September 3, 2011 09:36
Show Gist options
  • Save N-Carter/1190919 to your computer and use it in GitHub Desktop.
Save N-Carter/1190919 to your computer and use it in GitHub Desktop.
Adding a HingeJoint component
using UnityEngine;
using System.Collections;
public class StickOnContact : MonoBehaviour
{
public Collider m_Contactor;
void OnCollisionEnter(Collision collisionInfo)
{
foreach(ContactPoint contact in collisionInfo.contacts)
{
if(contact.thisCollider == m_Contactor)
{
HingeJoint hingeJoint = gameObject.AddComponent<HingeJoint>();
hingeJoint.connectedBody = contact.otherCollider.attachedRigidbody; // OK if it's null
hingeJoint.axis = Vector3.right;
hingeJoint.anchor = Vector3.down * 0.6f;
JointLimits limits = hingeJoint.limits;
limits.min = -5;
limits.minBounce = 0;
limits.max = 5;
limits.maxBounce = 0;
hingeJoint.limits = limits;
hingeJoint.useLimits = true;
break;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment