I need to keep a rigidbody in the same position and rotation. When it's being pushed by another rigidbody, it should try to return to its initial pose, with a force proportional to the distance / rotation delta from its initial pose. This code works very well for the position. The attached file is a short video showing the desired effect. The rigidbody rotation is locked. Code (CSharp): using UnityEngine; public class Keep_Physics_Pos : MonoBehaviour { private Vector3 startPos; public float force = 50; Rigidbody rb; // Start is called before the first frame update void Start() { rb = GetComponent<Rigidbody>(); if(rb == null) Destroy(this); Reset_Pos(); } public void Reset_Pos() { startPos = transform.position; } // FixedUpdate is called once per physics step void FixedUpdate() { float dist = Vector3.Distance(startPos, transform.position); if(dist > 0.01f) { rb.velocity = (startPos - transform.position) * force * dist; } } } I can't figure out how to do the same for the rotation. Thanks
If you don't want your Dynamic Rigidbody to move then is there a reason to not use its positions constraints or use a Kinematic/Static Rigidbody? Is it that you do want it to move but gradually move back to some fixed pose?
I do want it to move - in a way. It needs to act kind of like a soft mattress, resisting to external forces lightly at first and then more strongly. The attached video shows the effect I'm looking for. Using joints did not work, and since the position can be solved so easily, I wanted to find a similar solution for rotation.
Oh right, sorry I didn't pickup that the .zip file was a video for some reason. I'm not a 3D physics dev but I wonder if the 3D ConfigurableJoint offers enough utility or if you've tried it. You can set XYZ drives to a position and Angular XYZ drives to a rotation and set the force of the spring/damping etc. Got to step away now but I'll take a look tomorrow but I think I get the idea.