- Home /
How to rotate a GameObject with another GameObject while simulating gravity?
The idea is to simulate a 2D space environment where a spaceship (GameObject) gets pulled towards a planet (GameObject) and slowly rotates towards the center of the planet.
My current approach seems to simulate the gravitational pull, but it is very glitchy when it comes to the rotation:
Rigidbody2D rbToAttract = objToAttract.GetComponent<Rigidbody2D>();
Vector2 direction = rb.position - rbToAttract.position;
float distance = direction.magnitude;
// Position
float forceMagnitude = (rb.mass * rbToAttract.mass) / Mathf.Pow(distance, 2);
Vector2 force = direction.normalized * forceMagnitude;
rbToAttract.AddForce(force);
// Rotation
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.forward);
// NOTE: one option is to invert the direction and parent the object (far from practical)
objToAttract.transform.LookAt(-direction);
// NOTE: another option is to rotate it slowly with the other object
//objToAttract.transform.rotation = Quaternion.Slerp(objToAttract.transform.rotation,
rotation, Time.deltaTime);
Here is an illustration of what I'm trying to achieve: The black arrow resembles the spaceship. The green circle resembles the planet.
Any ideas on a better approach?
Answer by JasperDre · Mar 03, 2018 at 12:23 PM
I eventually fixed it after doing several tests with quaternion and vector functions and mathf from Unity. This is the result where the object gets pulled towards the other object while also rotated towards the center for easy landing. The script could be improved, but I'm working on a prototype (not final): Rigidbody2D rbToAttract = objToAttract.GetComponent();
Vector2 direction = new Vector2(transform.position.x, transform.position.y) - rbToAttract.position;
float distance = direction.magnitude;
// Position
float forceMagnitude = gravitationalPull * (mass * rbToAttract.mass) / Mathf.Pow(distance, 3);
Vector2 force = direction.normalized * forceMagnitude;
rbToAttract.AddForce(force);
// Rotation
float angle = Vector3.Angle(Vector3.up, objToAttract.transform.position - transform.position);
Debug.Log("angle " + angle);
float currentAngle = objToAttract.transform.eulerAngles.z;
if (objToAttract.transform.position.x > transform.position.x)
{
angle = -angle;
}
float finalAngle = Mathf.LerpAngle(currentAngle, angle, Time.deltaTime);
objToAttract.transform.eulerAngles = new Vector3(0, 0, finalAngle);
Answer by Rise2 · Feb 24, 2018 at 08:48 PM
Maybe you can use this:
public float countdown = 10.0f;
public float speedZ;
void Update()
{
countdown -= Time.deltaTime;
if (countdown <= 0.0f)
{
transform.Rotate(Vector3.forward * Time.deltaTime * speedZ);
}
}
Just an idea, hope it helps.
Hi, Your code is indeed similair to what I'm using to rotate the planet. The current issue is more related to the spaceship. The spaceship does get pulled towards the rotating planet, but it stays on that point and doesn't rotate or move from that position. $$anonymous$$y explantation might be a bit vague, but what I'm trying to achieve is to have the spaceship land on the planet, and make it stick to its relative landing point on the planet as they both should rotate.
Can you attach the spaceship directly to the planet in the hierarchy?
That works when the spaceship doesnt move around, but I would like to rotate it from any close point towards the center.
I don't know then, but you can try reposting the question and a little bit clearer. Sorry about that
Your answer
Follow this Question
Related Questions
Is there a way to lock velocity? 3 Answers
Rotate a weapon in a circle around player 1 Answer
C# Water Rotation 0 Answers
Flip over an object (smooth transition) 3 Answers