Flocking / Boids Collision

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?

hi,

I think adding just OnCollisionEnter to your player

and checking if the collided object tag is for example Boid (*You need to set tag to the boid prefab first)

Then can Destroy the boid object, inside OnCollisionEnter.

1 Like

Thanks for the help, worked great so ended up going with.

using UnityEngine;
using System.Collections;

public class Collisions : MonoBehaviour
{
    void OnCollisionEnter (Collision col)
    {
        if(col.gameObject.name == "Boid(Clone)")
        {
            Destroy(col.gameObject);
        }
    }
}

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.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

/// <summary>
/// these define the flock's behavior
/// </summary>
public class BoidController : MonoBehaviour
{
    public float minVelocity = 5;
    public float maxVelocity = 20;
    public float randomness = 1;
    public int flockSize = 20;
    public BoidFlocking prefab;
    public Transform target;

    internal Vector3 flockCenter;
    internal Vector3 flockVelocity;

    List<BoidFlocking> boids = new List<BoidFlocking>();

    void Start()
    {
        for (int i = 0; i < flockSize; i++)
        {
            BoidFlocking boid = Instantiate(prefab, transform.position, transform.rotation) as BoidFlocking;
            boid.transform.parent = transform;
            boid.transform.localPosition = new Vector3(
                            Random.value * GetComponent<Collider>().bounds.size.x,
                            Random.value * GetComponent<Collider>().bounds.size.y,
                            Random.value * GetComponent<Collider>().bounds.size.z) - GetComponent<Collider>().bounds.extents;
            boid.controller = this;
            boids.Add(boid);
        }
    }

    void Update()
    {
        Vector3 center = Vector3.zero;
        Vector3 velocity = Vector3.zero;
        foreach (BoidFlocking boid in boids)
        {
            center += boid.transform.localPosition; // THIS IS THE LINE IT'S RELATING TO
            velocity += boid.GetComponent<Rigidbody>().velocity;
        }
        flockCenter = center / flockSize;
        flockVelocity = velocity / flockSize;
    }
}
    void Update()
    {
        Vector3 center = Vector3.zero;
        Vector3 velocity = Vector3.zero;
        foreach (BoidFlocking boid in boids)
        if (this == null)
        {
            {
            center += boid.transform.localPosition;
                velocity += boid.GetComponent<Rigidbody>().velocity;
            }
        }
        flockCenter = center / flockSize;
        flockVelocity = velocity / flockSize;
    }
}

Nevermind, what I did earlier now seems to be working :slight_smile: Thanks again.

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).

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.

NullReferenceException: Object reference not set to an instance of an object
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

1 Like