First level!

Here’s my first level. I want the player to feel like a skilled space rocket pilot, so I created my own gravity system (which you can copy paste, I put it below the image), that’s why the level looks simple, it’s kinda hard to land on the green pad when that big orange is pulling you towards it.

I also liked the shadows in the background, it gives a weird feel to the level, like you are in space but not at all.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace GG.Core
{
    [RequireComponent(typeof(Rigidbody))]
    public class Gravity : MonoBehaviour
    {
        [SerializeField] float gravityStrength = 0;

        Rigidbody rgb;
        Transform planet;

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

        private void FixedUpdate() { ApplyGravity(); }

        private void ApplyGravity()
        {
            if (planet != null) { rgb.AddForce(CalculateGravity()); }
        }

        private Vector3 CalculateGravity()
        {
            Vector3 gravityDirection = (planet.position - transform.position).normalized;
            float gravityFullStrength = gravityStrength * (planet.localScale.x / Vector3.Distance(transform.position, planet.position));
            return gravityDirection * gravityFullStrength;
        }

        private void OnTriggerEnter(Collider other) //Sets the planet
        {
            if (other.CompareTag("Planet")) { planet = other.transform; }
        }

        private void OnTriggerExit(Collider other) //Removes the planet
        {
            if (other.CompareTag("Planet") && other.transform == planet) { planet = null; }
        }
    }
}

To implement this, you require to do the following things;

  • Create a “Planet” tag.
  • Add the script to the objects you want to be affected by gravity.
  • Objects with the script attached should have disabled the “Use gravity” option in their Rigidbodies.
  • The objects that will have gravity (that will pull objects), need a second collider set as trigger, it can be anything, a sphere, a box, a capsule, even an empty game object can work, you can create black holes if you want to. Also set their tag as “Planet”.

Just a little explanation onto what is going on in the “CalculateGravity” method.

  • Calculates the direction and normalizes it so we don’t get any weird behavior.
  • Then it calculates the gravity’s full strength based on the planet size and distance between the center of the planet and the Player. The bigger the planet and the closer to the ground the harder it will pull.
  • Then the values are multiplied to get the directional force we need.

If you want to implement it, go ahead, have fun with it!

1 Like

Why did I never think about adding a gravity system!? :open_mouth:

This is genius. Great explanation on how to implement Gravity code. You are very good at what you do. Keep it up Yee. You constantly impress me.

1 Like

Thanks :smile:

I tweaked the gravity system, the first intention was to keep it as simple as possible, but this way is so much better, it gives far more levers without changing the gravity mechanic.

using UnityEngine;

namespace GG.Core
{
    [RequireComponent(typeof(Rigidbody))]
    public class Gravity : MonoBehaviour
    {
        [Range(0, 500)][SerializeField] float density = 0;

        private void OnTriggerStay(Collider other)
        {
            if (other.gameObject.CompareTag("Gravity Target"))
            {
                other.GetComponentInParent<Rigidbody>().AddForce(CalculateGravity(other.transform));
            }
        }

        private Vector3 CalculateGravity(Transform t)
        {
            Vector3 direction = (transform.position - t.transform.position).normalized;
            Vector3 directionalGravityForce = direction * ((density * Time.deltaTime) / Vector3.Distance(transform.position, t.transform.position));
            return directionalGravityForce;
        }
    }
}

The implementation is pretty similar, but this time instead of putting the script on the objects that will be affected by gravity you put them on the things you want to have gravity.

The script is almost the same, the only difference is that you’ll need to tweak the density number, the bigger the number, the stronger the gravity, this means you can have very small objects that have super strong gravity, also I discovered something quite nice, the planet (or whatever) can pull multiple objects at the same time, Why? To be honest I have no idea, but it works for some reason.

Hi yee, wow how did you came up with this. This is unbelievable. I think you are a genius at maths

1 Like

Hi! Thanks. I’m no genius :sweat_smile: I think I still have much more to learn.

To be honest it was just trial and error and just have a little knowledge on how gravity works in space in a super basic way; supposedly, the denser an object the bigger the gravity pull will be, so I based everything on that principle, which isn’t really that hard to pull off once you get your head around how Rigidbodies work.

But still all those math working and physics it requires knowledge.

1 Like

For those interested in writing their own physics (i.e. gravity, wind resistance, etc.) @ben made a course way back using Unity 4.5 (just went to the course and discovered he updated it in 2019) Game Physics - Introducing Gravitation & Rotation in Unity unfortunately that course didn’t make it onto the GameDev course page. :man_shrugging:

1 Like

Hey thanks! That’s super cool, I’ll check it out later!

Privacy & Terms