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?
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;
}
}
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).