I've written a code that scans the level for Planets, then depending on the distance and their mass, it calculates a force of gravity that it applies to the SpaceShip. I'm using Newton's law of gravity: F=G*((M*m)/r^2). Since this is used for astronomical values, my ship would just get stuck on a Planet. So I reduced it to this: F=M*m/r. The problem is that the gravity is still too powerful. I have 2 Planets: One has 589.6456 in mass ,and the other one 2827.433. My spaceship has a mass of 3.90945, and I'm using auto-mass. Here is my code:
    using UnityEngine;
    using System.Collections;
    
    public class Grav : MonoBehaviour {
    
    	public float maxGravDist = 4.0f;
    	public float maxGravity = 35.0f;
    
    
    	GameObject[] planets;
    	public GameObject SpaceShip;
    
    	void Start () {
    		planets = GameObject.FindGameObjectsWithTag("Planet");
    	}
    
    	void FixedUpdate () {
    		foreach(GameObject Planet in planets) {
    			float dist = Vector3.Distance(Planet.transform.position, transform.position);
    				Vector3 v = Planet.transform.position - transform.position;
    				foreach(GameObject Planets in planets) {
    					float Planet_Mass = Planets.GetComponent ().mass;
    					GetComponent().AddForce(v.normalized * ((Planet_Mass*SpaceShip.GetComponent().mass)/dist));
    				}
    			}
    		}
    	}   
                       
                           
                       
                     ↧