Search Unity

Flocking / Boids Collision

Discussion in 'Physics' started by davii, May 9, 2015.

  1. davii

    davii

    Joined:
    May 9, 2015
    Posts:
    25
    Hey :)

    I'm using this script for a few items floating around my scene (C# version).

    http://wiki.unity3d.com/index.php?title=Flocking

    By default, the script has collision detection on the boids to avoid each other, which is fine. But, that collision detection is also applied to my player. (When you travel toward the boids they move out of the way, basically).

    Would anyone know how to adapt this script / add a script to make the player able to collide with the boids? I'm wanting to run into the boids and destroy them. Looking through the script / files I can't see anywhere to alter to make this happen?
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,428
    davii likes this.
  3. davii

    davii

    Joined:
    May 9, 2015
    Posts:
    25
    Thanks for the help, worked great so ended up going with.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Collisions : MonoBehaviour
    5. {
    6.     void OnCollisionEnter (Collision col)
    7.     {
    8.         if(col.gameObject.name == "Boid(Clone)")
    9.         {
    10.             Destroy(col.gameObject);
    11.         }
    12.     }
    13. }
    However, the game throws up this error:
    I get why it's saying that, but Not entirely sure how to check the if the BoidFlocking is Null? I've tried a lot of stuff off the forums (tried checking with if statements and stuff) but nothing seems to be working.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. /// <summary>
    6. /// these define the flock's behavior
    7. /// </summary>
    8. public class BoidController : MonoBehaviour
    9. {
    10.     public float minVelocity = 5;
    11.     public float maxVelocity = 20;
    12.     public float randomness = 1;
    13.     public int flockSize = 20;
    14.     public BoidFlocking prefab;
    15.     public Transform target;
    16.  
    17.     internal Vector3 flockCenter;
    18.     internal Vector3 flockVelocity;
    19.  
    20.     List<BoidFlocking> boids = new List<BoidFlocking>();
    21.  
    22.     void Start()
    23.     {
    24.         for (int i = 0; i < flockSize; i++)
    25.         {
    26.             BoidFlocking boid = Instantiate(prefab, transform.position, transform.rotation) as BoidFlocking;
    27.             boid.transform.parent = transform;
    28.             boid.transform.localPosition = new Vector3(
    29.                             Random.value * GetComponent<Collider>().bounds.size.x,
    30.                             Random.value * GetComponent<Collider>().bounds.size.y,
    31.                             Random.value * GetComponent<Collider>().bounds.size.z) - GetComponent<Collider>().bounds.extents;
    32.             boid.controller = this;
    33.             boids.Add(boid);
    34.         }
    35.     }
    36.  
    37.     void Update()
    38.     {
    39.         Vector3 center = Vector3.zero;
    40.         Vector3 velocity = Vector3.zero;
    41.         foreach (BoidFlocking boid in boids)
    42.         {
    43.             center += boid.transform.localPosition; // THIS IS THE LINE IT'S RELATING TO
    44.             velocity += boid.GetComponent<Rigidbody>().velocity;
    45.         }
    46.         flockCenter = center / flockSize;
    47.         flockVelocity = velocity / flockSize;
    48.     }
    49. }
     
  4. davii

    davii

    Joined:
    May 9, 2015
    Posts:
    25
    Code (CSharp):
    1.     void Update()
    2.     {
    3.         Vector3 center = Vector3.zero;
    4.         Vector3 velocity = Vector3.zero;
    5.         foreach (BoidFlocking boid in boids)
    6.         if (this == null)
    7.         {
    8.             {
    9.             center += boid.transform.localPosition;
    10.                 velocity += boid.GetComponent<Rigidbody>().velocity;
    11.             }
    12.         }
    13.         flockCenter = center / flockSize;
    14.         flockVelocity = velocity / flockSize;
    15.     }
    16. }
    Nevermind, what I did earlier now seems to be working :) Thanks again.
     
    Last edited: May 10, 2015
  5. roger_lew

    roger_lew

    Joined:
    Apr 21, 2015
    Posts:
    13
    I think the NullException is because in your BoidController script the boids list still contains a reference to the boid after it is destroyed by your Collisions component.

    You could have a public method in your BoidController to remove it from the list and destroy the boid (and even respawn a new one if so desired).
     
  6. arkhk

    arkhk

    Joined:
    Aug 17, 2020
    Posts:
    1
    Hello, I am using this script for flocking behavior in my scene (C# version) too.
    http://wiki.unity3d.com/index.php?title=Flocking

    but it throws up to this error below.
    Code (CSharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2. BoidController.Update () (at Assets/Scenes/BoidController.cs:44)
    Would anyone know how to adapt this script? Please use the shark models provide in the scene for boids.
    https://drive.google.com/file/d/1rwbdbeDp3FKBqv3ZJiqOfLQ6yxJ2ET75/view?usp=sharing
     
    zainabmohd13 likes this.