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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Basic fish AI

Discussion in 'Scripting' started by Yourking77, Jun 27, 2016.

  1. Yourking77

    Yourking77

    Joined:
    Jan 14, 2016
    Posts:
    303
    I need help creating ai for my fish, I need to know how I can keep them underwater and how to make the bass jump if they go out of water and how to make fish that land on shore flop around and die, and I would like to make it so my fish despawn after so long so my fish spawner can sort of refresh itself every so often so new fish can spawn.

    At the moment the fish spawns and I see it for a second then it is just gone. it disappears for some reason I need help with that as soon as possible.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class BassAi : MonoBehaviour {
    6.  
    7.     Animator bassAnimation;
    8.  
    9.     public bool predator = false;
    10.  
    11.     float changeTime = 10.0f;
    12.  
    13.     float movementSpeed = 1.0f;
    14.  
    15.     float movementChance = 50.0f;
    16.  
    17.     public float movementMin = 0.1f;
    18.     public float movementMax = 1.0f;
    19.  
    20.     float randomX = 0;
    21.     float randomY = 0;
    22.     float randomZ = 0;
    23.  
    24.     Vector3 bassVector = new Vector3(0, 0, 0);
    25.  
    26.     void Start() {
    27.         InvokeRepeating("bassMovement", 0, changeTime);
    28.     }
    29.  
    30.     void Update () {
    31.         //Handles the movement of the fish.
    32.         //There is a 25% chance the bass will stay in place.
    33.         if (movementChance > 25) {
    34.             movementSpeed = Random.Range(movementMin, movementMax);
    35.             transform.position += Vector3.forward * 0.0f * movementSpeed;
    36.         }
    37.  
    38.         //Handles the collision of the fish.
    39.         Ray bass = new Ray(transform.position, transform.forward);
    40.         RaycastHit hit;
    41.         if (Physics.Raycast(bass, out hit)) {
    42.             if (predator == true) {
    43.                 //Controls the fish eating prey.
    44.                 if (hit.transform.tag == "FishPrey") {
    45.                     transform.position += Vector3.forward * 10.0f * 3.0f;
    46.                     bassAnimation.Play("Eating");
    47.                 }
    48.             }
    49.             //Turns the fish if it hits a wall.
    50.             if (hit.transform.name == "cell") {
    51.                 transform.position += Vector3.back * Time.deltaTime * movementSpeed;
    52.             }
    53.             //Turns the fish if it hits a rock.
    54.             if (hit.transform.tag == "rock") {
    55.                 transform.position += Vector3.back * Time.deltaTime * movementSpeed;
    56.             }
    57.         }
    58.     }
    59.  
    60.     void bassMovement() {
    61.         changeTime = Random.Range(20.0f, 40.0f);
    62.  
    63.         movementChance = Random.Range(0, 100);
    64.  
    65.         movementSpeed = Random.Range(movementMin, movementMax);
    66.  
    67.         randomX = Random.Range(0, 360);
    68.         randomY = Random.Range(-15, 15);
    69.         randomZ = Random.Range(0, 360);
    70.  
    71.         bassVector = new Vector3(randomX, randomY, randomZ);
    72.     }
    73. }
     
    Last edited: Jun 27, 2016
  2. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    Using a raycast in the update() is very bad, it's two costly. I know you have concerns about this in the past. but good job on implementing the raycast and checking for a tag name.

    AI is a very complete task to create from your post. there are many ways to simulate AI. I would start by looking up Finite State Machine. Unity had a built in FSM, the animator.

    To despawn a fish you need to create a timer and if timer > float
    // Kills the game object
    Destroy (gameObject);
     
  3. listener

    listener

    Joined:
    Apr 2, 2012
    Posts:
    179
    Like johne5 said AI is very complex theme to just explain how in simple terms. I suggest you google and read about AIs and how they are made and you will come to the solution.

    Basically you will probably create fish controller that will just react to commands swim to x, jump, die, etc... and then you would write some AI script that would control fish depending on some set of rules you define and for example if fish swims close to the surface and enters some collider that you set it triggers jump animation etc.

    This is just very simplified explanation of what you need to do there is a lot of material out there about the topic.
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    nonsense
     
    Nitugard likes this.
  5. Yourking77

    Yourking77

    Joined:
    Jan 14, 2016
    Posts:
    303
    All I get when googling AI is enemy follow scripts and tutorials, does anybody have a good link to a simple tutorial I could use
     
  6. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    first thing to do would be to design the fish (make it move, make it jump, make it flop around), then design the decision making process it needs to make (I'm near the surface and random number says I should jump => jump, I'm near the surface and random number says stay => wiggle move, I'm near the surface and random number says DIVEDIVEDIVE! => move away from surface etc.). Once you've got those two things mapped out you can start to design the AI script.

    Decision making and mechanical "doing stuff" should really be separate.
     
    Last edited: Jun 27, 2016
  7. Yourking77

    Yourking77

    Joined:
    Jan 14, 2016
    Posts:
    303
    I created a new script and need help with something I placed markers and made the fish swim randomly between a set of markers, but the fish do not rotate at all how do I make them face the direction the are swimming?

    Edit: never mind that was insanely easy, I did not know there was a command that did that, a break and a fresh google search with a cleared mind helps a lot sometimes.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class BasicAI : MonoBehaviour {
    6.  
    7.     public Transform[] target;
    8.     Transform newTarget;
    9.  
    10.     Animator bassAnimation;
    11.  
    12.     bool isMoving = false;
    13.  
    14.     public float speed = 2.0f;
    15.  
    16.     public bool predator = false;
    17.  
    18.     void Update () {
    19.        if (isMoving == false) {
    20.             newTarget = target[Random.Range(0, target.Length)];
    21.             isMoving = true;
    22.         }
    23.  
    24.         transform.position = Vector3.MoveTowards(transform.position, newTarget.position, speed * Time.deltaTime);
    25.  
    26.         if (transform.position == newTarget.position) {
    27.             isMoving = false;
    28.         }
    29.  
    30.         transform.LookAt(newTarget);
    31.     }
    32. }
     
  8. Yourking77

    Yourking77

    Joined:
    Jan 14, 2016
    Posts:
    303
    I really need help with my spawner script now, it is changing the fish size as they are on screen i need it to spawn ones with random sizes how would I do that?

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class FishSpawnScript : MonoBehaviour {
    6.  
    7.     [Header ("Fish Spawner")]
    8.     public GameObject spawner;
    9.  
    10.     //Keeps track of the kind of fish the game needs to spawn.
    11.     [Header("Fish To Spawn")]
    12.     public GameObject bass;
    13.     public GameObject perch;
    14.  
    15.     //Keeps track of the kind of fish the spawner is allowed to spawn and their spawn chances.
    16.     [Header("Fish Models")]
    17.     public bool spawnBass = true;
    18.     public float bassSpawnChance = 20.0f;
    19.  
    20.     public float bassSizeMin = 0.5f;
    21.     public float bassSizeMax = 2.0f;
    22.  
    23.     public bool spawnPerch = true;
    24.     public float perchSpawnChance = 35.0f;
    25.  
    26.     public float perchSizeMin = 0.5f;
    27.     public float perchSizeMax = 2.0f;
    28.  
    29.     //Keeps track of how many fish have spawned.
    30.     [Header("Maximum Fish Allowed")]
    31.     public int fishMax = 25;
    32.     int fishCount = 0;
    33.  
    34.     //The time it takes to spawn a fish.
    35.     [Header("Fish Spawn Time")]
    36.     public float spawnMin = 0;
    37.     public float spawnMax = 32;
    38.  
    39.     float spawnTime = 0;
    40.  
    41.     float spawnRotation = 0;
    42.  
    43.     void Start() {
    44.         spawnTime = Random.Range(spawnMin, spawnTime);
    45.  
    46.         InvokeRepeating("spawnFish", 0, spawnTime);
    47.     }
    48.  
    49.     void spawnFish() {
    50.         fishSize();
    51.  
    52.         spawnTime = Random.Range(spawnMin, spawnTime);
    53.  
    54.         spawnRotation = Random.Range(1, 360);
    55.  
    56.         if (fishCount < fishMax) {
    57.             if (spawnBass == true) {
    58.                 var spawnChance = Random.Range(0, 100);
    59.                 if (spawnChance <= bassSpawnChance) {
    60.                     Instantiate(bass, spawner.transform.position, Quaternion.Euler(0, spawnRotation, 0));
    61.                     fishCount = fishCount + 1;
    62.                     return;
    63.                 }
    64.             }
    65.             if (spawnPerch == true) {
    66.                 var spawnChance = Random.Range(0, 100);
    67.                 if (spawnChance <= perchSpawnChance) {
    68.                     Instantiate(perch, spawner.transform.position, Quaternion.Euler(0, spawnRotation, 0));
    69.                     fishCount = fishCount + 1;
    70.                     return;
    71.                 }
    72.             }
    73.         }
    74.     }
    75.  
    76.     void fishSize() {
    77.         var bassX = Random.Range(bassSizeMin, bassSizeMax);
    78.         var bassY = Random.Range(bassSizeMin, bassSizeMax);
    79.         var bassZ = Random.Range(bassSizeMin, bassSizeMax);
    80.         Vector3 bassVector = new Vector3(1, 1, 1);
    81.  
    82.         bassVector.x = bassX;
    83.         bassVector.y = bassY;
    84.         bassVector.z = bassZ;
    85.  
    86.         bass.transform.localScale = bassVector;
    87.  
    88.         var perchX = Random.Range(perchSizeMin, perchSizeMax);
    89.         var perchY = Random.Range(perchSizeMin, perchSizeMax);
    90.         var perchZ = Random.Range(perchSizeMin, perchSizeMax);
    91.         Vector3 perchVector = new Vector3(1, 1, 1);
    92.  
    93.         perchVector.x = bassX;
    94.         perchVector.y = bassY;
    95.         perchVector.z = bassZ;
    96.  
    97.         perch.transform.localScale = perchVector;
    98.     }
    99. }
     
  9. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    Line 60, 68
    Code (CSharp):
    1.  
    2. GameObject NewFish = (GameObject) Instantiate(bass, spawner.transform.position, Quaternion.Euler(0, spawnRotation, 0));
    3.  
    4.  NewFish.transform.localScale = new Vector3(Random.Range(0.1f,5), Random.Range(0.1f, 5), Random.Range(0.1f, 5));
    5.  
     
  10. Yourking77

    Yourking77

    Joined:
    Jan 14, 2016
    Posts:
    303
    Thanks I got that half working with that code, it does not want to spawn perch with it but I am sure I can figure that out.
     
  11. ZapTurtle

    ZapTurtle

    Joined:
    Jul 2, 2018
    Posts:
    2
    Kindly guide me what's the problem here?
     
  12. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,366
    I wouldn't bother. This was from five years ago and it looks like it's already been solved.
     
  13. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133