Skip to content

Instantly share code, notes, and snippets.

@DraconInteractive
Last active August 8, 2022 07:49
Show Gist options
  • Save DraconInteractive/35f68969d79f1cf50c9aa4578963802d to your computer and use it in GitHub Desktop.
Save DraconInteractive/35f68969d79f1cf50c9aa4578963802d to your computer and use it in GitHub Desktop.
basic overview of a vr climbing mechanic
public Transform climbingAnchor;
public float climbRadius = 0.1f;
private ConfigurableJoint Joint;
void OnGrab ()
{
if (ClimbGrab)
{
// Player is now attached to climbable object
}
}
void OnRelease ()
{
if (Joint != null)
{
Destroy(Joint);
}
}
public bool ClimbGrab ()
{
// I use a singleton map to check all the different climbing colliders in the scene
// and see where the closest point to grab is.
// Easiest implementation is to find the collider of the object at your hand and get Collider.ClosestPoint(hand.position);
var closestPoint = ClimbPoint.Closest(climbingAnchor.position, climbRadius);
if (closestPoint != null)
{
// create and intialize a configurable joint at the closest point
Joint = gameObject.AddComponent<ConfigurableJoint>();
Joint.xMotion = Joint.yMotion = Joint.zMotion = ConfigurableJointMotion.Locked;
Joint.angularXMotion = Joint.angularYMotion = Joint.angularZMotion = ConfigurableJointMotion.Locked;
Joint.anchor = transform.InverseTransformPoint(climbingAnchor.position);
Joint.autoConfigureConnectedAnchor = false;
//retrieving the collider of the closest object
var hit = closestPoint.col;
// if possible, connect our joint to the object, so that as the object moves, so do we.
// otherwise, just attach to the air.
if (hit.attachedRigidbody)
{
Joint.connectedBody = hit.attachedRigidbody;
Joint.connectedAnchor = hit.attachedRigidbody.transform.InverseTransformPoint(climbingAnchor.position);
}
else
{
Joint.connectedAnchor = climbingAnchor.position;
}
return true;
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment