Uploaded November 2025 | Updated November 2026, 2 hours ago
Today we're going to be taking a look at creating spherical gravity systems in Unity. This will allow us to simulate realistic orbits of objects in our game or create three dimensional worlds that you can walk around and jump between.
The formula for newtons law of gravity is: `The Gravitational Constant * ((Mass of Body 1 * Mass of Body 2) / Distance Between Objects Squared)`
We can translate this into Unity by applying force to every Rigidbody in the scene using a function like this:
```csharp
foreach (var otherBody in otherBodies)
{
var distance = Vector3.Distance(transform.position, otherBody.transform.position);
var direction = (transform.position - otherBody.transform.position).normalized;
var gravityForce = gravity * ((body.mass * otherBody.mass) / (distance * distance));
otherBody.AddForce(direction * gravityForce * Time.deltaTime);
}
```
This function block iterates over every `otherBody` in a list of `otherBodies` (`RigidBody`'s) and calculates two values. The force applied to the `otherBody` based on the law of gravity and the vector of the force to apply to that object. Then we apply the gravitational force to the object using `AddForce`.
note: `Time.deltaTime` has been added to the code example to make physics consistent. I missed this in the video.
***
I've been working on a lot of IRL projects lately and only recently am getting back into some Unity related projects. Hopefully we'll be able to share more soon as I step back into doing more game dev related work.
***
Newton's Law of Gravity: en.wikipedia.org/wiki/Newton%27s_law_of_universal_gravitation
Unity Rigidbody Reference: docs.unity3d.com/ScriptReference/Rigidbody.html
Today we're going to be taking a look at creating spherical gravity systems in Unity. This will allow us to simulate realistic orbits of objects in our game or create three dimensional worlds that you can walk around and jump between.
The formula for newtons law of gravity is: `The Gravitational Constant * ((Mass of Body 1 * Mass of Body 2) / Distance Between Objects Squared)`
We can translate this into Unity by applying force to every Rigidbody in the scene using a function like this:
```csharp
foreach (var otherBody in otherBodies)
{
var distance = Vector3.Distance(transform.position, otherBody.transform.position);
var direction = (transform.position - otherBody.transform.position).normalized;
var gravityForce = gravity * ((body.mass * otherBody.mass) / (distance * distance));
otherBody.AddForce(direction * gravityForce * Time.deltaTime);
}
```
This function block iterates over every `otherBody` in a list of `otherBodies` (`RigidBody`'s) and calculates two values. The force applied to the `otherBody` based on the law of gravity and the vector of the force to apply to that object. Then we apply the gravitational force to the object using `AddForce`.
note: `Time.deltaTime` has been added to the code example to make physics consistent. I missed this in the video.
***
I've been working on a lot of IRL projects lately and only recently am getting back into some Unity related projects. Hopefully we'll be able to share more soon as I step back into doing more game dev related work.
***
Newton's Law of Gravity: en.wikipedia.org/wiki/Newton%27s_law_of_universal_gravitation
Unity Rigidbody Reference: docs.unity3d.com/ScriptReference/Rigidbody.html





. Well be working with a .NET console application, but this library can also be used in ASP.NET, Unity3D or other .NET projects. YamlDotNet provides a set of APIs for both serializing and deserializing YAML files, well be using these to parse a `tags.yml` file we provide into C# objects so that we can interact with them in our code.
### Example
If you want to parse a YAML file that looks like this:
```yml
user:
name: World of Zero
```
You would use this in C# to create a `Player` object that represents the yml data.
```csharp
public static Player ParsePlayer(string ymlContents) {
var deserializer = new DeserializerBuilder()
.WithNamingConvention(CamelCaseNamingConvention.Instance)
.Build();
return deserializer.Deserialize˂Player˃(ymlContents);
}
public class Player {
public User Username { get; set; }
}
public class User {
public string Name { get; set; }
}
```
### Installing YamlDotNet
YamlDotNet is available on [Nuget](https://www.nuget.org/packages/YamlDotNet) and can be installed from the `dotnet` command line using:
```sh
dotnet add package YamlDotNet
```
or via the Package Manager with
```sh
Install-Package YamlDotNet
```
Note: Make sure to check [Nuget](https://www.nuget.org/packages/YamlDotNet) for the latest available version.
***
More about YamlDotNet on its GitHub page: https://github.com/aaubry/YamlDotNet
You can find other YamlDotNet code samples at: https://github.com/aaubry/YamlDotNet/wiki/Samples
If youd like to use YamlDotNet to parse YAML in Unity3D you can use the free package from Unitys Asset Store: https://assetstore.unity.com/packages/tools/integration/yamldotnet-for-unity-36292
***
Join the World of Zero Discord Server: https://discord.gg/hU5Kq2u Learning to Deserialize YAML in C# with YamlDotNET](https://i.ytimg.com/vi/KXj5pAzmka8/mqdefault.jpg)


 over our character controller to allow us to build modular pieces instead of a single monolithic character controller. In this video well take advantage of that design to add NUnit tests over our basic character controller. This should help allow us to validate the behavior of our character controller and ensure future changes do not negatively impact the stability of our game.
Unity includes NUnit testing already - a framework I am not familiar with - well be exploring how to setup some basic tests, build out a few custom Mocks for our classes and test the basic top down controller we had previously built. At the end of this well have test coverage over the movement action of our character controller that allows us to validate our player moving.
This video is specifically looking at unit tests, integration and end-to-end tests are larger more complex problems that Im not sure how we want to cover yet.
More NUnit documentation is available on GitHub: https://github.com/nunit/docs/wiki/NUnit-Documentation
Join the World of Zero Discord Server: https://discord.gg/hU5Kq2u Introducing Testing into our Platformer](https://i.ytimg.com/vi/MVllsatsW5A/mqdefault.jpg)

