Rotating player to look in a fixed direction but on a planet (sphere world)
Hello I am new to unity and im having trouble with (probably) a simple concept.
I am trying to make a game where the player plays on a planet. I got the gravity down and can move the player around. The player also rotates so that he stands perpendicular to the surface beneath him. Now I am trying to make the player face the same direction as my right joystick is facing. This works when the player is standing up (world coordinates 0,1,0) but I can't get it to work when im moving my player around (for example when the player is standing at side of the planet).
Here is the code have so far:
public void Attract(Transform body) {
Vector3 gravityUp = (body.position - transform.position).normalized;
Vector3 bodyUp = body.up;
body.GetComponent<Rigidbody>().AddForce(gravityUp * gravity);
Quaternion targetRotation = Quaternion.FromToRotation(bodyUp,gravityUp) * body.rotation;
body.rotation = Quaternion.Slerp(transform.rotation, targetRotation, 50f * Time.deltaTime);
}
The planet script has this attract method and my player calls this method in his update() and passes his transform with it. I have tried a lot but don't have any working code yet for the look rotation so it would be amazing if someone could help me.
Your answer
![](https://koobas.hobune.stream/wayback/20220613010352im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Rotation fails to work correctly when the rotation of another object goes past a cetain point. 0 Answers
Wrong .rotation.eulerAngles.x output 1 Answer
What is wrong with my code? C# beginner 1 Answer
How do you change your z axis in a movement script. 1 Answer
How to access y rotation of an object as a variable or value 1 Answer