How to Simulate Gravity in Unity

Brackeys · 418K views · 2017 · 9:24

Open on YouTube

Game programming & enginesIndie game developmenttutorial

Why Stack Overflow recommends it

Stack Overflow users link it as the starting point for their Unity gravity and orbit code, then ask about bugs such as objects flinging away, unstable orbits or moving-planet movement.

Recommended by 0 people across 0 different months, 2020 to 2022.

What it is

A short Unity tutorial from Brackeys that applies Newton's law of universal gravitation to planets using Rigidbody forces in C#. It walks through a simple attractor script so bodies pull on each other and can orbit.

What people said

Cites Grant Fowler · 2020-05 · Stack Overflow · on “How do i make objects that are attracted to each other not bounce into oblivion in Unity3D?”

so I'm trying to make a program that allows asteroids in a field to be attracted to each other like this: [LINK] I have two programs to do this, one randomly generates asteroids, the other applies gravitational force to each object. the problem is that the asteroids will get faster as they get closer until they are bullet speed and the applied forces causes them to shoot away from each other and disappear. Is there a way to make them negate their forces on contact. I tried Rigidbody.isKinematic but that makes the asteroids still instead of rotating like they would in space. ill post the code below but here are the two projects I'm using the code from. [this video] [LINK] Code for asteroid spawner

Cites Valon Cazimi · 2022-01 · Stack Overflow · on “Simulate Gravity & Orbit in Unity”

Hey and thanks for your help in advanced. I've watched a few youtube videos on how to add Solar System and orbiting Gravity in Unity and ended up using the this for help for the solar system gravity part. [this video] But right after i decided to trying to make my planet orbit the sun i used this Wikipage for the math equation [LINK] But for some reason either my planets flies away of the sun start flying towards the planet. I've been looking around for 2 days and can't seem to make it work and tried diffrent type of possiblies. Here is my code public class Planets : MonoBehaviour { const float G = 100F; public Rigidbody rb; public float CurrentV; private void FixedUpdate() { Planets[] attractors

Cites Jack Freeman · 2022-07 · Stack Overflow · on “Unity player movement on moving planet”

I am making a game involving orbital physics. I was successfully able to implement this with a slightly modified version of Brackeys gravity tutorial [this video] , this is the code: using System.Collections; using System.Collections.Generic; using UnityEngine; public class gravity : MonoBehaviour { public GameObject self; public Rigidbody rb; public Vector3 initialVelocity; const float G = 66.74f; public static List<gravity> Attractors; public bool isAttractable; private void Awake() { rb.AddForce(initialVelocity); } private void FixedUpdate() { //planets if (isAttractable == false) { foreach (gravity attractor in Attractors) { if (attractor != this) Attract(attractor); } } //players, spaceships