Keep position of CharacterController relative to rotating platform
Dear community,
Inspired by the game Space Engineers, I would like to have a character that can walk around a moving and rotating spaceship. For the movement of the character I am using CharacterController. For the spaceship I use RigidBody as I found both of them to fit my needs perfectly. The Character moves with the spaceship, currently represented by a platform, like a charm. No problems there. However, I struggle a lot with the rotation.
I have a system which detects all characters in scope of the spaceship and parents those to the spaceship. This causes the rotation as represented by the quaternion to be perfectly aligned with the spaceship at all times. However, the position also needs to be rotated, as the CharacterController refuses to move together with the parent. Therefore I use a little code from the internet to calculate the rotated position of the character. On first glance, this works, but the character falls off the spaceship and slides around e.g. when doing a whole rotation around the Z-Achsis. Especially when rotating around multiple achses, this bears problems. This problem persists when disabling the code used for falling, also the spaceship itself rotates like a charm and all things parented to it that do not have a CharacterController attached do, too, so I suspect this being a problem of the rotation of the position of the CharacterController. Below my code.
The spaceship as a script called "SpaceshipGravitator" attached that handles the gravitation for all characters onboard the spaceship. Upon gravitation, the spaceship "adds up" all rotations that occur that fixed update in a quaternion, rotates itself and then hands the rotation to the gravitator, which is supposed to rotate the characters, handle falling etc.
public void OnStarshipRotated(Quaternion rotation)
{
foreach(PlayerMovement pm in pms)
{
Vector3 newPosition = rotation * (pm.cc.transform.position - transform.parent.position) + transform.parent.position;
Vector3 toBeMovedBy = newPosition - pm.cc.transform.position;
pm.cc.Move(toBeMovedBy);
}
}
Does someone have an idea how to solve my problem? I have browsed this and other forums for quite a while but no answer has solved my problem so far.
Thanks in advance,
Unitobinator