Hi there,
I wrote a little script to allow my first person controller to pick up and physically move around objects in the scene.
What I am trying to achieve is to rotate the object, so that it will always face the player with the same side.
What I do at the moment is this:
pickedObject.rigidbody.angularVelocity = Vector3.zero;
pickedObject.localRotation = Quaternion.Slerp(pickedObject.localRotation, transform.localRotation, 0.1f);
This behaves as expected, but it will always turn the front face towards the player, which can look quite awkward due to the fact that it has to turn 180° sometimes. My goal is to use the face that was selected by the player instead, which is determined via:
Vector3[] dirs = {Vector3.left, Vector3.right, Vector3.back, Vector3.forward, Vector3.up, Vector3.down}
float smallest = Mathf.Infinity;
for (int i = 0; i < dirs.Length; i++) {
float dP = Vector3.Dot(pickedObject.InverseTransformPoint(hit.point - pickedObject.rigidbody.centerOfMass),
pickedObject.InverseTransformDirection(dirs[i]));
if (dP < smallest) {
smallest = dP;
directionIndex = i;
}
}
As far as I know,
pickedObject.rotation * -dirs[directionIndex]
would then give me the world-coordinate direction of that side.
I just can’t figure out, how I have to change the code for rotating the object in such a way that this will be the side facing the player. Any suggestions will be greatly appreciated.
Greetings,
Pfaeff