Home > database >  Black Hole Physics ( 2D ) in unity
Black Hole Physics ( 2D ) in unity

Time:06-01

I'm trying to have a black hole like physics that will attract any object that will go close to it, then put it into the black hole orbit and like a vortex make it go to then center and disappear.

I ended up with something like this but doesn't work

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

[DisallowMultipleComponent]
[ExecuteInEditMode]
[RequireComponent(typeof(CircleCollider2D))]
[RequireComponent(typeof(SpriteRenderer))]
public class BlackHole : MonoBehaviour
{    
    /// <summary>  </summary>
    public const float          GRAVITY_PULL    = 7000.0f;
    /// <summary>  </summary>
    private const float         SWIRLSTRENGTH   = 5f;

    // ------------------------------------------------
    /// <summary>  </summary>
    private float               _gravityRadius  = 7.0f;    
    /// <summary>  </summary>
    private List<Rigidbody2D>   _rigidBodies    = new List<Rigidbody2D>();
    // ------------------------------------------------

#if UNITY_EDITOR     
    void Update()
    {
        if( Application.isPlaying == false )
        {
            _gravityRadius = 
                GetComponent<CircleCollider2D>().radius;
        }
    }
#endif 

    private void LateUpdate()
    {    
        UpdateBlackHole();
    }


    /// <summary>
    /// Attract objects towards an area when they come within the bounds of a collider.
    /// This function is on the physics timer so it won't necessarily run every frame.
    /// </summary>
    /// <param name="in_other">Any object within reach of gravity's collider</param>
    void OnTriggerEnter2D(
        Collider2D in_other
    )
    {
        if ( in_other.attachedRigidbody != null 
            && _rigidBodies != null )
        {                
            //to get them nice and swirly, use the perpendicular to the direction to the vortex
            Vector3 direction = 
                    transform.position - in_other.attachedRigidbody.transform.position;
            var tangent = 
                    Vector3.Cross(direction, Vector3.forward).normalized * SWIRLSTRENGTH;

            in_other.attachedRigidbody.velocity = 
                                            tangent;            
            _rigidBodies.Add( in_other.attachedRigidbody );
        }
    }

    private void UpdateBlackHole()
    {
        if( _rigidBodies != null )
        {
            for (int i = 0; i < _rigidBodies.Count; i  )
            {
                if( _rigidBodies[i] != null )
                {
                    CalculateMovement( _rigidBodies[i] );
                }
            }
        }
    }

    

    private void CalculateMovement(
        Rigidbody2D in_rb
    )
    {
        float distance = 
                Vector3.Distance(
                                transform.position, 
                                in_rb.transform.position
                            ); 

        float gravityIntensity = 
                        distance
                            / _gravityRadius;

        in_rb.AddForce(
                (transform.position - in_rb.transform.position) 
                                                        * gravityIntensity 
                                                                * in_rb.mass   
                                                                        * GRAVITY_PULL 
                                                                                * Time.deltaTime);
        
        in_rb.drag  = 0.0001f;
        
        Debug.DrawRay(
                in_rb.transform.position, 
                transform.position - in_rb.transform.position
                );


        if( distance <= 0.1f )
        {
            _rigidBodies.Remove( in_rb );

            Destroy( in_rb.gameObject );
        }
    }
}

this will make the object go up and down from the center of the black hole and increase it's speed that is something that I want but instead to go up and down I want to make it orbit around it and then get close to the center of the black hole during time.

any help?

Update 1: by adding a small force at the start I was able to make it rotate around the black hole, but it will not fall to the center of it

Update 2 :solved it by adding some drag, if someone is trying to achieve the same things feel free to use this script.

CodePudding user response:

you need to give the object some start force because that will give horizontal speed and make an orbit. To make it spiral inward, make the start force very small and it will fall towards the black hole with some amount of horizontal speed.

  • Related