How to make a central point of gravity

I want to make planets that have central points of gravity, but I cannot figure out how to. All I need is to be able to make round game objects have gravity like planets. Please help

Edit:

I am wondering how to make gravity that can put objects in orbit.

4 Answers

4

Try the following:

public class ObjectOnPlanet: MonoBehaviour
{

    private Rigidbody rigid;
   
    void Start()
    {
        rigid = GetComponent<Rigidbody>();
    }

    Vector3 planetGravity;

    private void FixedUpdate()
    {
        planetGravity = Quaternion.FromToRotation(Vector3.up,transform.position) * Physics.gravity;
        rigid.AddForce(planetGravity,ForceMode.Force);
    }

}

Basically we take the gravity vector and rotate it to align it to the vector that goes from the origin of the scene to the object position (basically transform.position).
That particular rotation is deduced by using the Quaternion.FromToRotation(Vector3.up, transform.position) because physics.gravity is by default aligned along Vector3.up in world space.

This is a really good answer, probably much better than the accepted solution. It's set up for a single gravity source, so anyone wanting to walk around the surface of a huge planet, this is the code ^

This is a fantastic starter code. Problem is, when used as-is, you still get some odd reactions. I noticed that all the objects I instantiated with this code attached gradually slid to the bottom of the "planet" (target object). If you add a line to disable regular gravity from the rigidbody, it works perfectly. Try: void Start() { rigid = GetComponent<Rigidbody>(); rigid.useGravity = false; }

I’m not sure how well sys12’s suggestion works for this purpose, but you can do some funny stuff with the physics system. The following is just quickly made out of nowhere, but it’s quite cool for orbit-like effects. You can tweak it way more to have your desired behaviour.

You can also code something similar from scratch which doesn’t invole the physics system, but just play around with it, maybe it inspires you a little bit:

(disable useGravity on the rigidbody)

using UnityEngine;
using System.Collections;

public class Gravity : MonoBehaviour
{

    public GameObject planet;
    private Rigidbody rb;

    [Range(0, 5)]
    public float factor = 1;
    public ForceMode mode;

    void Awake()
    {
        rb = GetComponent<Rigidbody>();
    }

    void FixedUpdate()
    {
        Vector3 g = (planet.transform.position - transform.position) * factor;
        rb.AddForce(g, mode);
    }
}

putting in the exact code other than the class name, i got the following errors error CS0178: Invalid rank specifier: expected ,' or ]' error CS1519: Unexpected symbol `float' in class, struct, or interface member declaration

It was because you did private RigidBody not private RigidBody = rb edit: What does the script do exactly

does this script work on making stuff orbit and crash into objects/planets

Thanks a lot @wccrawford I could'nt find this out myself, since it is ridiculous indeed....

Suppose, you have 2 types of objects : Big object & small objects.

Small objects revolves around big object. Add below script on small objects.

54448-gizmodraw.png

using UnityEngine;
using System.Collections;

public class CircularGravity : MonoBehaviour {
	
	public Transform target; // Big object
	Vector3 targetDirection;

	public int radius = 5;
	public int forceAmount = 100;
	public float gravity = 0;
	private Rigidbody rb;

	private float distance;

	void OnDrawGizmos() {
		Gizmos.color = Color.yellow;
		Gizmos.DrawWireSphere(transform.position, radius);
	}

	// Use this for initialization
	void Start () {
		Physics.gravity = new Vector3 (0, gravity, 0);
		rb = GetComponent<Rigidbody>();
	}
	
	// Update is called once per frame
	void Update () {

		targetDirection = target.position - transform.position; // Save direction
		distance = targetDirection.magnitude; // Find distance between this object and target object
		targetDirection = targetDirection.normalized; // Normalize target direction vector

		if(distance < radius) {
			rb.AddForce (targetDirection * forceAmount * Time.deltaTime);
		}


	}
}

Basically the same that i posted. I'm wondering if the OP even tried it out and did not just wait for a more complete version. Good job though, but I'd the AddForce line into FixedUpdate.

Check this out:

http://forum.unity3d.com/threads/finding-the-center-of-mass-of-a-collection-of-rigidbodies.87578/

It's ridiculous, but the "get resources" link won't appear until you've added a leaderboard or achievement, even if you don't want either of those things.