Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- public class BlackHoleScript : MonoBehaviour
- {
- // The strength of the black hole's gravitational pull
- public float gravityStrength = 50f;
- // The range within which objects are affected by the black hole
- public float gravityRadius = 10f;
- void FixedUpdate()
- {
- // Find all objects within a certain radius of the black hole
- Collider[] colliders = Physics.OverlapSphere(transform.position, gravityRadius);
- foreach (Collider nearbyObject in colliders)
- {
- // Get the Rigidbody component of the object
- Rigidbody rb = nearbyObject.GetComponent<Rigidbody>();
- if (rb != null)
- {
- // Calculate the direction from the object to the black hole
- Vector3 directionToBlackHole = transform.position - nearbyObject.transform.position;
- // Apply a force to pull the object towards the black hole
- float distance = directionToBlackHole.magnitude;
- float forceMagnitude = gravityStrength / distance; // Inverse square law (or adjust as needed)
- rb.AddForce(directionToBlackHole.normalized * forceMagnitude);
- }
- }
- }
- // Optional: Visualize the gravity radius in the editor
- void OnDrawGizmosSelected()
- {
- Gizmos.color = Color.blue;
- Gizmos.DrawWireSphere(transform.position, gravityRadius);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement