HOW TO ADD GRAVITY TO A OBJECT?
Hi everyone I wanted to add gravity to a object but not the way it is usually done. I want to add gravity to my object A that affects object B like stars and planets, where the planets are pulled by the gravitational force of the star.So if object B is too close to object A while moving it will collide with it, but if it is at a proper distance then it will be affected by the gravity but it will not collide , only change its direction.THE IMAGE BELOW WILL MAKE IT EASY TO UNDERSTAND WHAT I WANT. So I have no idea how to make this possible, please help me out. NOTE It is a 2d game. thank you
Answer by Jessespike · Oct 17, 2015 at 04:57 PM
Implement Newton's law of universal gravitation. Wikipedia has a lot of jargon, You can probably find a simpler explanation somewhere else:
Force (F) of gravitational attraction between two objects with Mass1 and Mass2 at distance D is:
F = G(mass1*mass2)/D squared.
(G is the gravitational constant, which has the same value throughout our universe.)
So basically, create a script for your objects that have a Mass and a Velocity. And have another script that will use the formula to calculate the relative forces, you just then update the object's velocity based on the calculated force and direction.
Edit ---
If you're looking for a simpler solution, just for the example that you've provvided. You can probably just have 1 object move towards a point and if the objects get too close, then you change the target that the object moves towards. Psuedo example:
if (Vector3.Distance(star.transform.position, planet.transform.position) < 1f)
{
// objects are within proximity
targetPosition = star.transform.position; // set targetPosition to the star
}
else
{
// objects are not close enough to each other
targetPosition = someOtherPosition; // set targetPosition to some reference
}
planet.transform.position = Vector3.MoveTowards(planet.transform.position, targetPosition);
Answer by bubzy · Oct 17, 2015 at 07:24 PM
well, this kinda works, maybe you can tweak it. (its for 3d sorry but the principle is the same)
using UnityEngine;
using System.Collections;
public class gravityTest : MonoBehaviour {
// Use this for initialization
public float influence;
public float mass;
public bool isPlayable = false;
float speed = 1; //not sure how you will find this out, it may be easier in 2d?
Rigidbody body;
void Start () {
body = gameObject.GetComponent<Rigidbody> ();
}
// Update is called once per frame
void Update () {
if (isPlayable) { //this is for testing purposes however you control the object is up to you.
if (Input.GetKeyDown (KeyCode.A)) {
body.AddForce (new Vector3 (0, 10f, 0));
}
}
Collider[] hitColliders = Physics.OverlapSphere(transform.position, influence);
int i = 0;
while (i < hitColliders.Length) {
Debug.Log(hitColliders[i].name);
if(hitColliders[i].tag == "planet")
{
gravityTest test;
test = hitColliders[i].gameObject.GetComponent<gravityTest>();
if(test.mass > mass)
{
body.AddForce((hitColliders[i].transform.position - transform.position) * speed * Time.smoothDeltaTime);
}
}
i++;
}
}
}
this simply attracts an object to another, the one with the smaller mass being attracted to the larger one, you have to tag the objects "planet". see how you get on with it :)
Thank you, it did work , but not in the way I expected. I used it in 3d , so it does move upward and gets influenced by the gravity. but there is no change in the direction as shown in the figure above.Any idea how I can bring in that change.
fiddle with the numbers :) it does function like your diagram under certain conditions. (this is an approximation of the real physics though, im way too dumb to understand how it actually works)