Advertisement
Hygcgggnngff

Ass hole

Aug 3rd, 2024
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.45 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class BlackHoleScript : MonoBehaviour
  4. {
  5.     // The strength of the black hole's gravitational pull
  6.     public float gravityStrength = 50f;
  7.  
  8.     // The range within which objects are affected by the black hole
  9.     public float gravityRadius = 10f;
  10.  
  11.     void FixedUpdate()
  12.     {
  13.         // Find all objects within a certain radius of the black hole
  14.         Collider[] colliders = Physics.OverlapSphere(transform.position, gravityRadius);
  15.  
  16.         foreach (Collider nearbyObject in colliders)
  17.         {
  18.             // Get the Rigidbody component of the object
  19.             Rigidbody rb = nearbyObject.GetComponent<Rigidbody>();
  20.  
  21.             if (rb != null)
  22.             {
  23.                 // Calculate the direction from the object to the black hole
  24.                 Vector3 directionToBlackHole = transform.position - nearbyObject.transform.position;
  25.  
  26.                 // Apply a force to pull the object towards the black hole
  27.                 float distance = directionToBlackHole.magnitude;
  28.                 float forceMagnitude = gravityStrength / distance; // Inverse square law (or adjust as needed)
  29.                 rb.AddForce(directionToBlackHole.normalized * forceMagnitude);
  30.             }
  31.         }
  32.     }
  33.  
  34.     // Optional: Visualize the gravity radius in the editor
  35.     void OnDrawGizmosSelected()
  36.     {
  37.         Gizmos.color = Color.blue;
  38.         Gizmos.DrawWireSphere(transform.position, gravityRadius);
  39.     }
  40. }
  41.  
Tags: Script
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement