Search Unity

How Do I Specify A Specific Area To Place Objects/Fish?

Discussion in 'Scripting' started by Hawk0077, Nov 23, 2017.

  1. Hawk0077

    Hawk0077

    Joined:
    Nov 6, 2017
    Posts:
    788
    Hi, following on from my recent post requesting help about fish location/coordinates which produced a great solution by the pros (im a beginner)....

    I now need to specify a specific area/size of the area the fish will flock within.

    eg: The code below has a TankSize which is bsaically square and so fish go through the floor and out of the water which I solved by reducing the TankSize.

    However, I would like to specify a more specific TankSize to avoid this.
    Here is the current code which specifies a square area (I believe?)...

    Code (csharp):
    1.  
    2. public static int tankSize = 35;
    3.  
    4.     static int numFish = 250;
    5.     public static GameObject[] allFish = new GameObject[numFish];
    6.  
    7.     public static Vector3 goalPos = genfishglobalFlock.goalPos;
    8.  
    9.     // Use this for initialization
    10.     void Start()
    11.     {
    12.         for (int i = 0; i < numFish; i++)
    13.         {
    14.             Vector3 pos = this.transform.position +
    15.                       new Vector3(Random.Range(-tankSize, tankSize),
    16.                                   Random.Range(-tankSize, tankSize),
    17.                                   Random.Range(-tankSize, tankSize));
    18.             allFish[i] = (GameObject)Instantiate(fishPrefab, pos,
    19.                                             Quaternion.identity);
    20.  
    The exact coordinates of those fish is: -2147f, 15f, 465f

    Basically the sea level is 50 and so a tank size of 35 is kinda too large as the fish dissapear below the terrain or above the sea level so in the current settings have opted for below the map/terrain.

    Is there a way to specify x y and z for the tank size?

    As I write this I am having the idea of instead of using 35 as the tank size use something like: x=55,y=20,z=55 but that actually doesnt work as you no doubt know.

    Anyways, I appreaciate any guidance to solve the issue. Many thanks.

    PS; I like to get my hands a little dirty to get some ideas before reading massive amounts of text and so will study these areas more when I get to see live examples of how this C# stuff works.
    I have only just started to learn so please make any responses understandable to a newbie. Many thanks. Have a good one.[/CODE]
     
  2. Kaart

    Kaart

    Joined:
    Jul 31, 2017
    Posts:
    62
    Instead of manualy specifying the size of the fishtank you can use the localScale of the fishtank Transform to find the min/max spawn area for the fish. Use the code below and set the fishTank GameObject in the Inspector like you probably did for the fishPrefab.

    Code (CSharp):
    1. public class FishSpawn : MonoBehaviour
    2. {
    3.     public GameObject fishPrefab;
    4.     public GameObject fishTank;
    5.  
    6.     static int numFish = 250;
    7.     public static GameObject[] allFish = new GameObject[numFish];
    8.  
    9.     // Use this for initialization
    10.     void Start()
    11.     {
    12.         // Find the min/max values for the fish to spawn.
    13.         // We subtract the fishPrefab size from it to prevent fish spawning inside the walls of the fishtank.
    14.         float maxX = (fishTank.transform.localScale.x / 2) - fishPrefab.transform.localScale.x;
    15.         float maxY = (fishTank.transform.localScale.y / 2) - fishPrefab.transform.localScale.y;
    16.         float maxZ = (fishTank.transform.localScale.z / 2) - fishPrefab.transform.localScale.z;
    17.  
    18.         for (int i = 0; i < numFish; i++)
    19.         {
    20.             Vector3 pos = this.transform.position +
    21.                       new Vector3(Random.Range(-maxX, maxX),
    22.                                   Random.Range(-maxY, maxY),
    23.                                   Random.Range(-maxZ, maxZ));
    24.             allFish[i] = (GameObject)Instantiate(fishPrefab, pos,
    25.                                             Quaternion.identity);
    26.         }
    27.     }
    28. }
    The result is something like this (with fish instead of cubes):

     
  3. Hawk0077

    Hawk0077

    Joined:
    Nov 6, 2017
    Posts:
    788
    Thanks for the response Kaart, sorry for my delayed response but I have been working. I will look into that tomorrow as it will most likely make for better performance with the several flocks I have but I also wanted to specify coordinates so as to spread fish over a wider area as well as avoid the bottom of the map and the sea level.

    Saying that I have been working all day on a tutorial to set fish going through random waypoints which will solve my problem. Together with your response here the whole thing should perform better so thanks. Much appreciated. .
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    There is code in your original post (from the tutorial) that creates the vector3 based on a random range from negative fishtank to positive fish tank (ie: -35 - 35) for each part of the vector3.. it's repeated.
    If you wanted the smaller numbers, just make 3 float variables, or make a vector3 tank size (with your own dimensions).
    Using that, you can spawn them in a random location based on your new values, and keep them in the new dimensions (keeping 'em in the tank**). :)
     
    Hawk0077 likes this.
  5. Hawk0077

    Hawk0077

    Joined:
    Nov 6, 2017
    Posts:
    788
    Thanks for the response methos5k, I will play around with what you say and dig a little deeper. Much appreciated.
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    No problem :)
     
    Hawk0077 likes this.