Moving and Rotating a Player Smoothly on a Rotating Planet
Hey everyone, i'm having a hard time figuring this out and hope somebody will be able to help me.
I made some planets with a custom gravity for each one, which attracts the player to the center of mass. My problem is when i try to create a moon orbiting around a planet, because if the player on the moon doesn't rotate with it (basically the moon rotates under him, the player stands still and stays on the surface because of the gravity attraction).
Here's an image to explain it a bit better:
Here's the main code around this problem:
This is how i make the moon rotate around the planet:
transform.RotateAround(planet.position, rotationAxis, speed*Time.deltaTime);
And this is how the player is attracted to the planet he's standing on. (It's a mess, basically i get the normal of the vector between the player position and the center of mass, and adjust the gravity to attract the player there):
float distance = Vector3.Distance(currentAttractor.transform.position, transform.position);
Vector3 surfaceNorm = Vector3.zero;
RaycastHit hit;
if(Physics.Raycast(transform.position, currentAttractor.gravitationalCenter - transform.position, out hit, distance, layerMask)){
surfaceNorm = hit.normal;
}
transform.localRotation = Quaternion.FromToRotation(transform.up, surfaceNorm) * transform.rotation;
float pullForce = 0.0f;
float divider = Mathf.Pow(Vector3.Distance(currentAttractor.gravitationalCenter, transform.position), 2);
pullForce = currentAttractor.gravity / 11;
Vector3 pullVec = transform.position - currentAttractor.gravitationalCenter;
var vel = playerRigidBody.velocity;
vel.y -= Mathf.Abs(vel.y) * Time.fixedDeltaTime * extraGravity;
vel.x -= (pullForce*pullVec.normalized).x;
vel.y -= (pullForce*pullVec.normalized).y;
vel.z -= (pullForce*pullVec.normalized).z;
playerRigidBody.velocity = vel;
How would you fix this problem? I tried switching up my code a bit but without any success,. What i want is just that the player rotates with the moon (as it would happen in real life), but with my way of implementing gravity i feel like this is impossible. Am i missing something?
Did you ever find an answer to this? I'm having the same issue with my space game.
Your answer
Follow this Question
Related Questions
Unity Faux gravity on a cube: reaching some faces is troublesome 0 Answers
Gravity on a cube planet 1 Answer
I need to create artificial gravity ( like a fake fake centrifugal effect) 2 Answers
3D Aimable Sling Shot 0 Answers
Gravity on a round planet for character,Character not rotating towards planet surface 0 Answers