Rotate player based on custom axis
Ok, so I am making a game with some small planets the player can explore. Because the world is spherical, I had to make a custom gravity system. Anyway, I recycled some player movement code from an older project. The problem is that the way I coded the player to look around assumes it is on a flat world because it just rotates on the y axis. This will break the code that forces the player's feet to always face the ground, regardless of position on the planet. How can I modify this code?
void Update()
{
GetInput();
cam.transform.localRotation = Quaternion.Euler(xRotation, 0, 0);
transform.localRotation = Quaternion.Euler(0, yRotation, 0);
}
void GetInput()
{
mouseX = Input.GetAxisRaw("Mouse X");
mouseY = Input.GetAxisRaw("Mouse Y");
yRotation += mouseX * mouseSensitivity;
xRotation -= mouseY * mouseSensitivity;
xRotation = Mathf.Clamp(xRotation, -85f, 85f);
}
Here is a code snippet for the gravity
void MovePlayer()
{
rb.rotation = Quaternion.FromToRotation(transform.up, (transform.position - planet.transform.position).normalized) * transform.rotation;
rb.AddForce((transform.position - planet.transform.position).normalized * -10);
rb.AddForce(moveDirection.normalized * moveSpeed, ForceMode.Acceleration);
}
Any suggestions for improving either section would be greatly appreciated. I looked into the rotate around method but it does not seem like the right thing to use.
Your answer
![](https://koobas.hobune.stream/wayback/20220613052425im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Rotating player around rotating planet,Rotation of player on a rotating planet 0 Answers
I use click to move, but don't want my player to rotate... How to fix this rotation? 1 Answer
Rotating an object in one axis with camera and another axis with torque 0 Answers
How to make camera moving as the player on a planet 0 Answers
Check the entry point of a ship in planet's orbit and tell if movement is in rotation direction (2D) 1 Answer