This is a VR game and this script is for stabbing objects into other objects, like a nail in a wooden plank. Essentially, when I disable the collisions on the rigidbody the constraints on the configurable joint no longer function so I need to manually reset the position of the stabber every frame. I thought the best way to do so would be using Vector3.Project, and it looked promising for upward stabbing, but the moment I started stabbing from the sides the stabber was moving upward instead of into the stabbable.
Essentially, the red circle is the point that the stabber initially entered, but if something were to push it to the side, like seen above, I need a way to keep it at the same depth that it is currently at while also moving it back over so that it still falls within the entryPoint.
Going with an object entering from below, like in the picture works just fine, but when it’s rotated 90 degrees it suddenly stops working and keeps pushing it upward.
Here’s the code I was using for the position reset:
body.constraints = RigidbodyConstraints.FreezeRotation;
Vector3 posDiff = endPoint.position - _entryPoints[_stabbed[0]];
Vector3 onNormal = (_entryPoints[_stabbed[0]] + _stabberDirection);
Vector3 positionLock = Vector3.Project(posDiff * 1000, onNormal) / 1000;
transform.position = positionLock + _stabberEntry;