Hey there,
I am currently trying to make a first person arena shooter which takes place on Planetoids like many levels in Mario Galaxy (and it’s sequel).
Basically, there are many smaller planets which you can fly to with a jetpak. My problem now is that i can not get the rotation of my player right. If i get into the “atmosphere” of another planet, i want my player to slowly change his rotation so that he lands on his feet. I already had a solution but there the player’s rotation was set instantly and i want it to be a fluid movement.
public class FauxGravityAttractor : MonoBehaviour {
float distance;
float gravitationConstant = 300;
Rigidbody rigidbody;
float gravity;
float gravi;
public Text DistanceGreen;
public float GravityRotationRate = 30.0f;
void Awake () {
rigidbody = GetComponent<Rigidbody> ();
float mass = rigidbody.mass;
gravi = - gravitationConstant * mass;
}
public void Attract(Rigidbody body) {
Vector3 gravityUp = (body.position - transform.position).normalized;
Vector3 localUp = body.transform.up;
Vector3 radius = (transform.position - body.position);
distance = radius.magnitude;
SetDistanceText ();
gravity = ((float)gravi * body.mass) /(distance*distance);
// Apply downwards gravity to body
body.AddForce(gravityUp * gravity);
// Allign bodies up axis with the centre of planet. Works but when changing the planet, the player's rotation is set instantly.
body.rotation = Quaternion.FromToRotation (localUp, gravityUp) * body.rotation;
//Player is shaking and laid back, but fluid transition when changing planet.
body.rotation = Quaternion.RotateTowards (body.rotation, Quaternion.LookRotation (localUp, gravityUp), Time.deltaTime* GravityRotationRate);
}
Basically this is the function that attracts the Gravity Bodies to the planets. If i use the FromToRotation function, the rotation will be set instantly when changing the planet, but the player will stand up on the surface of a planet. With the RotateTowards method, the player will continously shake and he won’t stand up completely but rather be “laid back”. I attached some pictures so you guys can see what i mean.
Does anyone have a solution or any tips for me? I have a really hard time figuring out Quaternions and how i have to use them to work properly.
Thanks in advance. I really appreciate it!