Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to position fish in a specific location

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

  1. Hawk0077

    Hawk0077

    Joined:
    Nov 6, 2017
    Posts:
    788
    Hi, I am not a coder but am just starting to learn and am having a slight issue.

    I have been following a tutorial about fish but the fish were in a tank.

    The position of the fish was referred to as: Vector3.zero and the fish now swim underneath my terrain in the center of the map.

    but I would like the fish to be at a specific location within my terrain. E.G. (-1806,20,426)

    How can I change the position of the fish?

    I have tried replacing the .zero with .(-1806,20,426) and without the "." but that doesnt seem to work.

    You can see the tutorial here:


    Any help appreciated, thanks.
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Vector3 is a structure that holds 3 floats (float = number which can have floating point (decimal) values). It contains some convenient functions and properties that are commonly associated with Vector math. "Vector3.zero" is a property that holds the value (0, 0, 0) for convenience.

    You could also write that as "new Vector3(0, 0, 0)", but it saves you having to create a new one.

    That being said, you could set that to any values you want, so to create a brand new Vector3 and assign it to a Vector3 variable it would look like this:
    Code (CSharp):
    1. Vector3 myNewVector = new Vector3(-1806, 20, 426);
    I highly recommend looking through some beginner C# tutorials, as well as the tutorials here to get a grip on the basics of coding in C# and Unity:
    https://unity3d.com/learn/tutorials/s/scripting
     
  3. Hawk0077

    Hawk0077

    Joined:
    Nov 6, 2017
    Posts:
    788
    thanks for the response, I plan to dig a lot deeper but was just trying to fix the positioning issue I am having here then as I learn I might actually produce something. But thanks. Much appreciated.
     
  4. Hawk0077

    Hawk0077

    Joined:
    Nov 6, 2017
    Posts:
    788
    changing all instances of Vector3.zero to: new Vector3(-1806, 20, 426); doesnt cause any console errors but the fish still located under the terrain in the center?
     
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Post your script using forum code tags and people can take a look. Right now we have to just guess what is in your script. Note that the fish won't change position until the script is executed, so if they are scene objects they will be in the position listed in the inspector when you click on them until your script moves them.
     
  6. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    That is a very dangerous way to go about making changes haha. If you don't know what a number is doing, you probably shouldn't be changing it.

    If you have fully implemented the code they have in the tutorial, then the fish will follow whatever object you have provided to the "goalPrefab" field in the inspector for the Global Flock component. In the tutorial they show the fish following a sphere object around after adding that in the inspector.
     
  7. Hawk0077

    Hawk0077

    Joined:
    Nov 6, 2017
    Posts:
    788
    Thanks Joe Censored,

    Here is the first script called globalFlock:

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class globalFlock : MonoBehaviour {
    7.  
    8.     public GameObject fishPrefab;
    9.     public static int tankSize = 5;
    10.  
    11.     static int numFish = 100;
    12.     public static GameObject[] allFish = new GameObject[numFish];
    13.  
    14.     public static Vector3 goalPos = Vector3.zero;
    15.  
    16.     public GameObject myCoral;
    17.  
    18.     // Use this for initialization
    19.     void Start()
    20.     {
    21.         goalPos = myCoral.transform.position;
    22.  
    23.     {
    24.         for (int i = 0; i < numFish; i++)
    25.             {
    26.                 Vector3 pos = new Vector3(Random.Range(-tankSize, tankSize),
    27.                                           Random.Range(-tankSize, tankSize),
    28.                                           Random.Range(-tankSize, tankSize));
    29.                 allFish[i] = (GameObject)Instantiate(fishPrefab, pos, Quaternion.identity);
    30.             }
    31.         }
    32.     }
    33.     // Update is called once per frame
    34.     void Update ()
    35.     {
    36.         if (Random.Range(0, 10000) < 50) ;
    37.      
    38.  
    39.     }
    40. }
    41. [CODE]
    42.  
    43. Here is the second script called flocks:
    44.  
    45. [CODE]
    46. using System.Collections;
    47. using System.Collections.Generic;
    48. using UnityEngine;
    49.  
    50. public class flock : MonoBehaviour
    51. {
    52.  
    53.     public float speed = 0.5f;
    54.     float rotationSpeed = 4.0f;
    55.     Vector3 averageHeading;
    56.     Vector3 averagePosition;
    57.     float neighbourDistance = 3.0f;
    58.  
    59.     bool turning = false;
    60.  
    61.     // Use this for initialization
    62.     void Start()
    63.     {
    64.         speed = Random.Range(0.5f, 1);
    65.     }
    66.  
    67.     // Update is called once per frame
    68.     void Update()
    69.     {
    70.         if (Vector3.Distance(transform.position, Vector3.zero) >= globalFlock.tankSize)
    71.         {
    72.             turning = true;
    73.         }
    74.         else
    75.             turning = false;
    76.         if (turning)
    77.         {
    78.             Vector3 direction = Vector3.zero - transform.position;
    79.             transform.rotation = Quaternion.Slerp(transform.rotation,
    80.                                     Quaternion.LookRotation(direction),
    81.                                     rotationSpeed * Time.deltaTime);
    82.             speed = Random.Range(0.5f, 1);
    83.         }
    84.         else
    85.         {
    86.             if (Random.Range(0, 5) < 1)
    87.                 ApplyRules();
    88.         }
    89.         transform.Translate(0, 0, Time.deltaTime * speed);
    90.     }
    91.     void ApplyRules()
    92.     {
    93.         GameObject[] gos;
    94.         gos = globalFlock.allFish;
    95.         Vector3 vcentre = Vector3.zero;
    96.         Vector3 vavoid = Vector3.zero;
    97.         float gSpeed = 0.1f;
    98.  
    99.         Vector3 goalPos = globalFlock.goalPos;
    100.  
    101.         float dist;
    102.  
    103.         int groupSize = 0;
    104.         foreach (GameObject go in gos)
    105.         {
    106.             if (go != this.gameObject)
    107.             {
    108.                 dist = Vector3.Distance(go.transform.position, this.transform.position);
    109.                 if (dist <= neighbourDistance)
    110.                 {
    111.                     vcentre += go.transform.position;
    112.                     groupSize++;
    113.                     if (dist < 1.0f)
    114.                     {
    115.                         vavoid = vavoid + (this.transform.position - go.transform.position);
    116.                     }
    117.                     flock anotherFlock = go.GetComponent<flock>();
    118.                     gSpeed = gSpeed + anotherFlock.speed;
    119.                 }
    120.             }
    121.         }
    122.         if (groupSize > 0)
    123.         {
    124.             vcentre = vcentre / groupSize + (goalPos - this.transform.position);
    125.             speed = gSpeed / groupSize;
    126.  
    127.             Vector3 direction = (vcentre + vavoid) - transform.position;
    128.             if (direction != Vector3.zero)
    129.                 transform.rotation = Quaternion.Slerp(transform.rotation,
    130.                                                         Quaternion.LookRotation(direction),
    131.                                                         rotationSpeed * Time.deltaTime);
    132.         }
    133.     }
    134. }
    135.  
    I have changed this from the original to include a prefab of the "myCoral" and attached that to the globalFlock script in the inspector. As mentioned in one of here replies to the above video. However, the script still shows the fish under the hill in the centre of the terrain.

    As seen in the image below: The fish are currently at .zero under the green hill.


    Much appreciated, thanks.
     
  8. Hawk0077

    Hawk0077

    Joined:
    Nov 6, 2017
    Posts:
    788

    Thanks, yes in her reply to FuryFight3r under the video she goes on to give instructions to place the fish in a specific location. I did this (or tried to) as mentioned in the upgraded/changed code. Here instructions were:

     
  9. Hawk0077

    Hawk0077

    Joined:
    Nov 6, 2017
    Posts:
    788
    Thanks Joe-Censored, I replied and have added the code above. Thanks again.
     
  10. AlfredEnglund

    AlfredEnglund

    Joined:
    Apr 17, 2017
    Posts:
    5
    Make an variable "vector3" and add your coordinates, then set the position to the vector 3, good luck
     
  11. Hawk0077

    Hawk0077

    Joined:
    Nov 6, 2017
    Posts:
    788
    Thanks...

    I changed the code by changing the loop in the Start function of globalFlock.cs to:
    Code (csharp):
    1.  
    2. for(int i = 0; i < numFish; i++) { Vector3 pos = this.transform.position +                                         new Vector3(Random.Range(-tankSize,tankSize),                           Random.Range(-tankSize,tankSize),                           Random.Range(-tankSize,tankSize)); allFish[i] = (GameObject) Instantiate(fishPrefab, pos,                                  Quaternion.identity); }
    3.  
    and so now the fish start at my given coordinates but they swim off to the .zero point

    I have tried changing all Vector3.zero references to:

    1 - Vector3.globalFlock.goalPos
    2 - globalFlock.goalPos
    3 - Vector3(-1806,35,426)

    But neither work.

    bump!

    Best get beginners guide to C# Part 2.
     
  12. Hawk0077

    Hawk0077

    Joined:
    Nov 6, 2017
    Posts:
    788
    Thanks for all the inout, much appreciated you did push me in the right direction and I now solved it by replacing all the Vector3.sero instances with;
    new Vector3 (0.0f, 1.0f, 0f);

    of course I used my coordinates.

    Just wonder not how I can create 3-5 groups of fish within the same coordinates, but Il work on this one for a while to try and clean the behaviour up a little. Thanks again.