In my game, I had a previous movement script that worked. However, due to some reasons I cannot attach the script to the player directly, meaning the movement does not work anymore because it isn’t relative to the camera rotation (ex. pressing “W” always makes you go forward in the z axis, does not matter where you are looking in game). While I cannot attach the script directly to the player, I still have access to the player’s camera rotation.
Here is the old code:
controller.Move(playerVelocity * Time.deltaTime);
Here are some solutions I have tried so far:
controller.Move(Vector3.Project(FlattenVector3(playerVelocity), FlattenVector3(camProxy.transform.forward)) * Time.deltaTime);
controller.Move(new Vector3(0, playerVelocity.y, 0) * Time.deltaTime);
Another one:
controller.Move(new Vector3(0, playerVelocity.y, 0) * Time.deltaTime);
controller.Move((camProxy.transform.right * playerVelocity.x + Vector3.Normalize(FlattenVector3(camProxy.transform.forward)) * playerVelocity.z) * Time.deltaTime);
The second solution kinda works, except I dont stop moving for some reason (if anybody is able to fix that, that solution would work too). The jumping thing also works, those lines of code don’t need fixing.