using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class GrabPhysics : MonoBehaviour
{
public InputActionProperty grabInputSorce;
public float radius = 0.1f;
public LayerMask grabLayer;
private ConfigurableJoint fixedJoint;
private bool isGrabbing = false;
bool dontGrab = false;
// Update is called once per frame
void FixedUpdate()
{
bool isGrabButtonPressed = grabInputSorce.action.ReadValue<float>() > 0.5f;
if (isGrabButtonPressed && !isGrabbing && !dontGrab)
{
Collider[] nearbyColliders = Physics.OverlapSphere(transform.position, radius, grabLayer, QueryTriggerInteraction.Ignore);
if (nearbyColliders.Length > 0)
{
Rigidbody nearbyRigidbody = nearbyColliders[0].attachedRigidbody;
fixedJoint = gameObject.AddComponent<ConfigurableJoint>();
fixedJoint.autoConfigureConnectedAnchor = false;
fixedJoint.xMotion = ConfigurableJointMotion.Locked;
fixedJoint.yMotion = ConfigurableJointMotion.Locked;
fixedJoint.zMotion = ConfigurableJointMotion.Locked;
/*fixedJoint.angularXMotion = ConfigurableJointMotion.Locked;
fixedJoint.angularYMotion = ConfigurableJointMotion.Locked;
fixedJoint.angularZMotion = ConfigurableJointMotion.Locked;*/
JointDrive drive = new JointDrive();
drive.positionSpring = 6000;
drive.positionDamper = 100;
fixedJoint.angularXDrive = drive;
fixedJoint.angularYZDrive = drive;
//fixedJoint.rotationDriveMode = RotationDriveMode.Slerp;
if (nearbyRigidbody)
{
fixedJoint.connectedBody = nearbyRigidbody;
fixedJoint.connectedAnchor = nearbyRigidbody.transform.InverseTransformPoint(transform.position);
}
else
{
fixedJoint.connectedAnchor = transform.position;
}
isGrabbing = true;
}
else
{
dontGrab = true;
}
}
else if (!isGrabButtonPressed && isGrabbing)
{
isGrabbing = false;
if (fixedJoint)
{
Destroy(fixedJoint);
}
}
else if (!isGrabButtonPressed) {
dontGrab = false;
}
}
}
When i run this code (for vr) the object i pick up stays where i hold it but falls down and hangs downward even though i rotate my hand. If i set angularMotion to locked then it is very floppy even though the mass is just 1. It does not work with slerp rotation mode either.