Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Object Rendering based on Distance from all Enemies. Help Needed!

Discussion in 'Scripting' started by Mr_fuzion, Mar 26, 2018.

  1. Mr_fuzion

    Mr_fuzion

    Joined:
    Jul 3, 2013
    Posts:
    10
    Hi guys this is similar to my last post but i couldn't come to a proper conclusion so here I am. Basically i have a rather large game scene that contains lots of planet objects however for memory saving i disable all the objects out of both the player and the enemies range. Unfortunately the script i use limits me to having a player and a single enemy but what i require is a single player but multiple enemies. The script also doesn't work with just a player in the scene which also requires a fix. any help would be amazing thanks.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System.Linq;
    5.  
    6. public class PlanetRendering : MonoBehaviour {
    7.  
    8.     public GameObject[] planetLifes;
    9.     public GameObject[] planetCarbons;
    10.     public GameObject[] planetQuartz;
    11.     public GameObject currentEnemy;
    12.     public GameObject enemy;
    13.     public GameObject planetSpawner;
    14.     public List<GameObject> planetsInScene = new List<GameObject>();
    15.     public GameObject[] enemiesInScene;
    16.     public GameObject player;
    17.     public float renderDistance;
    18.  
    19.     private float positionX;
    20.     private float positionY;
    21.     private Vector3 newPosition = new Vector3();
    22.  
    23.     // Use this for initialization
    24.     void Start () {
    25.  
    26.         Invoke ("ListPlanets", 2f);
    27.         planetSpawner = GameObject.FindGameObjectWithTag ("PlanetSpawner");
    28.  
    29.     }
    30.  
    31.     void ListPlanets(){
    32.         enemiesInScene = GameObject.FindGameObjectsWithTag ("Enemy");
    33.         planetCarbons = GameObject.FindGameObjectsWithTag ("PlanetCarbon");
    34.         planetLifes = GameObject.FindGameObjectsWithTag ("PlanetLife");
    35.         planetQuartz = GameObject.FindGameObjectsWithTag ("PlanetQuartz");
    36.         player = GameObject.FindGameObjectWithTag ("Player");
    37.  
    38.         planetsInScene = planetLifes.Concat (planetCarbons).Concat (planetQuartz).ToList ();
    39.  
    40.     }
    41.  
    42.     void RenderPlanetsPlayer(){
    43.  
    44.         for (int i = 0; i < planetsInScene.Count; i++) {
    45.             for (int j = 0; j < enemiesInScene.Length; j++) {
    46.                 if (planetsInScene [i] != null && enemiesInScene[j] != null) {
    47.                     float distPlayer = (player.transform.position - planetsInScene [i].transform.position).magnitude;
    48.                     float distEnemy = (enemiesInScene[j].transform.position - planetsInScene [i].transform.position).magnitude;
    49.                     if (distPlayer < renderDistance || distEnemy < renderDistance) {
    50.                         planetsInScene [i].SetActive (true);
    51.  
    52.                         if (planetsInScene [i].GetComponent<PlanetLife> () != null) {
    53.                             planetsInScene [i].GetComponent<PlanetLife> ().inRender = true;
    54.                             planetsInScene [i].SetActive (true);
    55.                         } else if (planetsInScene [i].GetComponent<PlanetCarbon> () != null) {
    56.                             planetsInScene [i].GetComponent<PlanetCarbon> ().inRender = true;
    57.                             planetsInScene [i].SetActive (true);
    58.                         } else if (planetsInScene [i].GetComponent<PlanetQuartz> () != null) {
    59.                             planetsInScene [i].GetComponent<PlanetQuartz> ().inRender = true;
    60.                             planetsInScene [i].SetActive (true);
    61.                         }
    62.                     } else if (distPlayer > renderDistance && distEnemy > renderDistance) {
    63.                         if (planetsInScene [i].GetComponent<PlanetLife> () != null) {
    64.                             if (planetsInScene [i].GetComponent<PlanetLife> ().inRender == false) {
    65.                                 planetsInScene [i].GetComponent<PlanetLife> ().inRender = false;
    66.                             } else if (planetsInScene [i].GetComponent<PlanetCarbon> () != null) {
    67.                                 planetsInScene [i].GetComponent<PlanetCarbon> ().inRender = false;
    68.                             } else if (planetsInScene [i].GetComponent<PlanetQuartz> () != null) {
    69.                                 planetsInScene [i].GetComponent<PlanetQuartz> ().inRender = false;
    70.                             }
    71.                         }
    72.                         planetsInScene [i].SetActive (false);
    73.                     }
    74.                 }
    75.             }
    76.         }
    77.     }
    78.  
    79.     // Update is called once per frame
    80.     void Update () {
    81.  
    82.         //RenderPlanetsEnemy ();
    83.         RenderPlanetsPlayer ();
    84.  
    85.         }
    86. }
    87.  
     
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Your code already has an array of enemies in scene.
     
    karl_jones likes this.
  3. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,358
    That RenderPlanetsPlayer method could be one of the worst methods I have seen in a long time. Atleast 3 levels of if statements
     
  4. Mr_fuzion

    Mr_fuzion

    Joined:
    Jul 3, 2013
    Posts:
    10
    I am very new too coding and do not have any doubt that your statement is correct. any advice as to a better way of doing this?
     
  5. Mr_fuzion

    Mr_fuzion

    Joined:
    Jul 3, 2013
    Posts:
    10
    It does yes but once multiple enemies are added to the scene only one of them is able to activate the closest planets the rest do not do anything with surrounding planets.
     
  6. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,358
    Maybe break up the functionally in seperate components instead of using the raw gameobjects. And move statements there. Thats at least a start