Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine.UI;
- using System.Collections.Generic;
- public class DistanceOptimizer : MonoBehaviour
- {
- public Transform player;
- public float renderDistance = 100f;
- public float simulationDistance = 150f;
- public List<GameObject> excludedObjects;
- public List<MonoBehaviour> excludedScripts;
- private void Update()
- {
- if (player == null)
- return;
- // Calculate the squared distances
- float sqrRenderDistance = renderDistance * renderDistance;
- float sqrSimulationDistance = simulationDistance * simulationDistance;
- // Find all horses and riders
- GameObject[] horses = GameObject.FindGameObjectsWithTag("Horse");
- GameObject[] riders = GameObject.FindGameObjectsWithTag("Rider");
- // Loop through all of the horses
- foreach (GameObject horse in horses)
- {
- // Calculate the distance between the horse and the player
- float sqrHorseDistance = (horse.transform.position - player.position).sqrMagnitude;
- // Loop through all riders
- foreach (GameObject rider in riders)
- {
- // Calculate the squared distance between the rider and the player
- float sqrRiderDistance = (rider.transform.position - player.position).sqrMagnitude;
- // Determine the closest distance to the player between the horse and rider
- float sqrClosestDistance = Mathf.Min(sqrHorseDistance, sqrRiderDistance);
- // Disable rendering if beyond render distance
- Renderer renderer = rider.GetComponent<Renderer>();
- if (renderer != null)
- {
- renderer.enabled = sqrClosestDistance <= sqrRenderDistance;
- }
- // Disable simulation if beyond simulation distance
- foreach (MonoBehaviour script in rider.GetComponents<MonoBehaviour>())
- {
- if (script != this)
- {
- script.enabled = sqrClosestDistance <= sqrSimulationDistance;
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement