Help with orbits

I’m rather new to Unity and C#, and I’m trying to create a script that can set a 2d object into a circular orbit around another 2d object before any frame updates, regardless of positions or mass. I have a second functional script that automatically applies a gravitational force on all objects using G. Here’s what I have so far:

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

public class OrbitInitializer : MonoBehaviour
    // Hold G in this script
    private static float G;
    // Body to orbit
    public Rigidbody2D toOrbit;
    // This objects rigidbody
    public Rigidbody2D rb;
    // Used to determine direction of orbit
    public bool IsClockwise = false;

    // Start is called before the first frame update
    void Start()
        // Fetch the Rigidbody2D of the object to orbit
        toOrbit = GetComponent<Rigidbody2D>();

        Vector2 toOrbitPos = toOrbit.transform.position; // Find position of body to orbit
        Vector2 toOrbitDir = toOrbit.transform.position - rb.transform.position; // Find direction body to orbit is in

        float dist = toOrbitDir.magnitude; // Find distance from body to orbit (center) to this
        float mass = toOrbit.mass; // Find the mass of the body to orbit

        // 90 degree rotation
        Quaternion rotation = Quaternion.Euler(0, 0, 90);

        // Swap direction of orbit from counter-clockwise to clockwise
        if (IsClockwise == true)
            rotation = Quaternion.Euler(0, 0, -90);

        // Direction to apply force for orbit
        Vector2 orbitDir = rotation*toOrbitDir;
        orbitDir = orbitDir.normalized;

        // Define this scripts G as the G in Attractor.cs
        G = Attractor.G;

        // Magnitude of orbital force
        double velocity = Math.Sqrt((G * mass) / dist);  

        // Final calculation for orbit vector
        Vector2 orbit = orbitDir * (float)velocity;

        // Add force
        rb.velocity = orbit;

Whenever I run a game with this script attached to one of two “planets”, I receive an error message:

Rigidbody2D.velocity assign attempt for 'planet' is not valid. Input velocity is { NaN, NaN }.

Odds are my problem is something simple I’ve overlooked, but any help is appreciated.

Your issue doesn’t lie in the code you posted. I ran your code in a test project and it worked just fine.

The problem is either in the Attractor code or in the interaction between the two. If you post the Attractor code we’ll have a fighting chance :wink:

EDIT: Problem was with the OrbitInitializer code after all.

Your problem was with this line:

         // Fetch the Rigidbody2D of the object to orbit
         toOrbit = GetComponent<Rigidbody2D>();

Instead of fetching the target’s rigidbody, you were fetching your own rigidbody. As a result you were telling the rigidbody to orbit around itself. (Distance was 0 and you were dividing by distance, thus resulting in NaNs)