Commit d8bf7113 authored by christopher.spray's avatar christopher.spray

First Commit

parents
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Boid
{
public Flocking flock;
public float cohesionRadius = 10f; //radius for the cohesion rule
public float separationRadius = 3f; //radius for the separation rule
public float allignmentRadius = 5f; //radius for alignment rule
public float fleeRadius = 5f; //radius for fleeing rule
public float obstacleRadius = 5f; //radius for the obstacle avoidance rule
GameObject predator;
GameObject[] obstacles;
//3 basic properties each boid must have
public Vector2 velocity = Vector2.zero;
public Vector2 position = Vector2.zero;
public Vector2 acceleration = Vector2.zero;
public GameObject enemy;
public float size;
public Boid(Vector2 velocity, Vector2 position, Flocking flock, float size, GameObject enemy)
{
this.velocity = velocity;
this.position = position;
this.flock = flock;
this.enemy = enemy;
this.size = size;
}
public Vector2 Cohesion()
{
Vector2 averagePosition = new Vector2(0,0);
int boidAmount = 0;
for (int i = 0; i < this.flock.boidList.Count; i++) //for every boid
{
Vector2 dist = this.position - this.flock.boidList[i].position; //calc distance
if (dist.magnitude < cohesionRadius && this.flock.boidList[i] != this) //if it is close enough, and is NOT this one
{
averagePosition += this.flock.boidList[i].position; //add this boids pos to average pos
boidAmount++; //add 1 to amount of boids nearby
}
}
if (boidAmount > 0) //if any boids are nearby
{
averagePosition = averagePosition / boidAmount; //get the average position of the boids that are nearby only
averagePosition = (averagePosition - this.position) / 100; //work out steering force
//averagePosition.Normalize();
}
return averagePosition;
}
public Vector2 Separation()
{
Vector2 directionToMove = Vector2.zero;
int boidAmount = 0;
for (int i = 0; i < this.flock.boidList.Count; i++) //for every boid
{
if (this.flock.boidList[i] != this)
{
Vector2 dist = (this.position + new Vector2(size/2, size/2)) - (this.flock.boidList[i].position + new Vector2(this.flock.boidList[i].size/2, this.flock.boidList[i].size / 2));
if (dist.magnitude < separationRadius) //if it is close enough, and is NOT this one
{
boidAmount++;
directionToMove += (this.position - this.flock.boidList[i].position);
}
}
}
if (boidAmount > 0) //if any boids are nearby
{
directionToMove = directionToMove / boidAmount; //get the direction to move
//directionToMove.Normalize();
}
return directionToMove;
}
//Setting the alignment of the boid
public Vector2 Alignment()
{
Vector2 averageVelocity = Vector2.zero; //create empty average velocity
int boidAmount = 0; //create empty value to store how many boids are nearby
for (int i = 0; i < this.flock.boidList.Count; i++) //for every boid
{
float dist = Vector2.Distance(this.position, this.flock.boidList[i].position); //check it's distance
if (dist < allignmentRadius && this.flock.boidList[i] != this) //if it is close enough, and is NOT this one
{
averageVelocity += this.flock.boidList[i].velocity; //add that boids velocity to the average
boidAmount++; //add 1 to amount of boids nearby
}
}
if (boidAmount > 0) //if any boids are nearby
{
averageVelocity = averageVelocity / boidAmount; //get the average velocity of the boids that are nearby only
}
else
{
averageVelocity = this.velocity; //else keep the same velocity
}
averageVelocity = (averageVelocity - this.velocity) / 50; //work out steering force
//averageVelocity.Normalize();
return averageVelocity;
}
public Vector2 Flee()
{
Vector2 newHeading = Vector2.zero;
predator = GameObject.FindGameObjectWithTag("Player"); //get player obj
Vector2 predPos = predator.transform.position; //get player pos
float distToPred = Vector2.Distance(this.position, predPos); //check it's distance
if(distToPred < fleeRadius) //if its too close
{
//go to the negative direction of the predator
newHeading = (this.position - predPos);
}
return newHeading;
}
public Vector2 AvoidObstacle()
{
Vector2 newDir = Vector2.zero;
obstacles = GameObject.FindGameObjectsWithTag("Obstacle");
foreach (GameObject obstacle in obstacles)
{
Vector2 obstaclePos = obstacle.transform.position;
float distToWall = Vector2.Distance(this.position, obstaclePos);
if (distToWall < obstacleRadius)
{
}
}
return newDir;
}
}
fileFormatVersion: 2
guid: 0269a09e7c6de954aa6dabe5e43b22a6
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraMovement : MonoBehaviour
{
public Transform playerPos;
public float lerpSpeed;
public float camOffset;
// Update is called once per frame
void Update()
{
Vector3 offset = new Vector3(playerPos.position.x, playerPos.position.y, playerPos.position.z + camOffset);
transform.position = Vector3.Lerp(transform.position, offset, lerpSpeed * Time.deltaTime);
}
}
fileFormatVersion: 2
guid: 4b8218a065ba22e49b664a4786a3baee
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Flocking : MonoBehaviour
{
public GameObject enemy;
public GameObject parent;
public Boid boid;
public float vlim;
public int boidAmount;
public float speed;
public GameObject player;
//public float scatterRange;
public int edgeDirChange;
[SerializeField]
public List<Boid> boidList = new List<Boid>();
int width = 100;
int height = 100;
int state1 = 1;
int state2 = -20;
int m1 = 1;
int m2 = 1;
int m3 = 1;
int m4 = 1;
int m5 = 1;
public void Start()
{
for (int i=0; i<boidAmount; i++)
{
//apply random values
Vector2 velocity = new Vector2(Random.Range(-vlim, vlim), Random.Range(-vlim, vlim)); //set a random velocity
Vector2 position = new Vector2(Random.Range((-width/2)+1, (width/2)-1), Random.Range((-height/2)+1, (height/2)-1)); //set a random position
float size = Random.Range(0.5f, 2f); //give a random size
GameObject newBoidObj = Instantiate(enemy, position, Quaternion.identity); //create an object for the boid
newBoidObj.gameObject.transform.localScale = new Vector3(size, size, size); //set the objects size
Boid newBoid = new Boid(velocity, position, this, size, newBoidObj); //create a boid with a reference to the object created
boidList.Add(newBoid); //add that boid to a list
}
}
public void Update()
{
//rules
Vector2 cohesion = Vector2.zero;
Vector2 separation = Vector2.zero;
Vector2 alignment = Vector2.zero;
Vector2 flee = Vector2.zero;
Vector2 avoidObstacle = Vector2.zero;
int xMin = -width / 2;
int xMax = width / 2;
int yMin = -height / 2;
int yMax = height / 2;
if (Input.GetKey("space"))
{
m1 = state2;
}
else
{
m1 = state1;
}
for (int i = 0; i < boidList.Count; i++) //for every boid
{
Vector2 newPos = Vector2.zero;
//get each value for each boid
cohesion = m1 * boidList[i].Cohesion();
separation = m2 * boidList[i].Separation();
alignment = m3 * boidList[i].Alignment();
flee = m4 * boidList[i].Flee();
boidList[i].velocity = boidList[i].velocity + cohesion + separation + alignment + flee;
//limit the speed
boidList[i].velocity = LimitVelocity(boidList[i].velocity);
newPos = boidList[i].position += (boidList[i].velocity.normalized * speed) * Time.deltaTime;
//Keep it scrolling on x axis
if ((boidList[i].position).x > xMax)
{
boidList[i].position.x = -width / 2;
}
else if ((boidList[i].position).x < xMin)
{
boidList[i].position.x = width / 2;
}
if(newPos.y > yMax)
{
boidList[i].velocity.y -= edgeDirChange;
}
else if (newPos.y < yMin)
{
boidList[i].velocity.y += edgeDirChange;
}
boidList[i].position += (boidList[i].velocity.normalized * speed) * Time.deltaTime;
boidList[i].enemy.transform.position = boidList[i].position;
boidList[i].enemy.transform.rotation = Quaternion.LookRotation(boidList[i].velocity);
}
}
public Vector2 LimitVelocity(Vector2 velocity)
{
if (velocity.magnitude > vlim)
{
Vector2 clamped = Vector2.ClampMagnitude(velocity, vlim);
velocity = clamped;
}
return velocity;
}
}
fileFormatVersion: 2
guid: 1d00f5cb2f2854a46acd49154b04a100
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float speed = 0f;
public Light light;
public Vector3 startSize = new Vector3(1f, 1f, 1f);
public Flocking flock;
int width = 100;
int height = 100;
private void Start()
{
transform.localScale = startSize;
flock = GameObject.FindGameObjectWithTag("Controller").GetComponent<Flocking>();
}
// Update is called once per frame
void Update()
{
//Debug.Log(flock.boidList.Count);
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
int xMin = -width / 2;
int xMax = width / 2;
int yMin = -height / 2;
int yMax = height / 2;
Vector3 pos = transform.position;
Vector3 movement = new Vector3(horizontal, vertical, 0f) * speed * Time.deltaTime;
Vector3 nextPos = pos += movement * speed;
if (nextPos.x > xMax)
{
pos.x = xMin;
}
else if (nextPos.x < xMin)
{
pos.x = xMax;
}
else if (nextPos.y > yMax - transform.localScale.y)
{
pos.y = yMax - 1;
}
else if (nextPos.y < (yMin + transform.localScale.y))
{
pos.y = yMin + 1;
}
transform.position = pos;
transform.position += movement * speed;
light.transform.position = new Vector3(transform.position.x, transform.position.y, -30f);
Vector2 dirToBoid = Vector2.zero;
}
public void OnCollisionEnter(Collision collision)
{
Debug.Log("Collide");
GameObject collidedBoid = collision.gameObject;
for(int i=0; i < flock.boidList.Count; i++)
{
if(flock.boidList[i].enemy == collidedBoid)
{
Destroy(flock.boidList[i].enemy);
flock.boidList.RemoveAt(i);
}
}
}
}
fileFormatVersion: 2
guid: 39927b5971e0515479160daa7c71cdd3
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment