protected virtual void Update() { Vector2 smoothPosition = Flocking.SmoothVector(prevPos, Position); Vector2 smoothVelocity = Flocking.SmoothVector(prevVel, Velocity); this.transform.SetPositionAndRotation(smoothPosition, Quaternion.Euler(0, 0, -90 + Mathf.Rad2Deg * Mathf.Atan2(smoothVelocity.y, smoothVelocity.x))); }
void createCreature() { GameObject flockCharGO = Instantiate(CreaturePrefab) as GameObject; flockCharGO.transform.position = transform.position + transform.forward * 3; flockCharGO.transform.localPosition += new Vector3(Random.Range(0, 2), 0, Random.Range(-3, 3)); // Set up seek flockCharGO.GetComponent<Seek>().seekTarget = seekTarget; // Set up flocking flocking = flockCharGO.GetComponent<Flocking>(); flocking.weight = weight; flocking.aoe = aoe; flocking.priority = priority; flocking.neighborRadius = neighborRadius; flocking.useSeperation = useSeperation; flocking.seperationWeight = seperationWeight; flocking.useAlignment = useAlignment; flocking.alignmentWeight = alignmentWeight; flocking.useCohesion = useCohesion; flocking.cohesionWeight = cohesionWeight; numCreatures--; if(numCreatures==0) DestroyImmediate(this); }
void Awake() { steeringBehaviours = GetComponents <SteeringBehaviour>(); Wander wander = GetComponent <Wander>(); if (wander != null) { initialWanderWeight = wander.weight; } Flocking flocking = GetComponent <Flocking>(); if (flocking != null) { initialAlignmentWeight = flocking.alignmentWeight; initialCohesionWeight = flocking.cohesionWeight; initialSeperationWeight = flocking.seperationWeight; } // Get the physical collider (not the trigger) BoxCollider2D[] boxColliders = GetComponents <BoxCollider2D>(); foreach (BoxCollider2D bc in boxColliders) { if (!bc.isTrigger) { boxCollider = bc; break; } } }
private void Awake() { if (instance == null) { instance = this; } }
public override void OnEnter() { _flocking = (Flocking)((FSMBeetle)this.Fsm).beetleFlocking; _beetle = (BeetleBehaviur)((FSMBeetle)this.Fsm).beetle; _lineOfSight = (LineOfSight)((FSMBeetle)this.Fsm).beetleLineOfSight; _lineOfSight.setExitedBehaviour(); }
private void Flock() { var oobVectorWeight = 0.2f; var ships = World.BodiesNear(Position, World.Hook.FlockCohesionMaximumDistance) .OfType <Ship>(); var flockingVector = Vector2.Zero; var oobVector = Vector2.Zero; if (ships.Count() > 1) { flockingVector = (World.Hook.FlockCohesion * Flocking.Cohesion(ships, this, World.Hook.FlockCohesionMaximumDistance)) + (World.Hook.FlockAlignment * Flocking.Alignment(ships, this)) + (World.Hook.FlockSeparation * Flocking.Separation(ships, this, World.Hook.FlockSeparationMinimumDistance)); } if (IsOOB) { if (Position != Vector2.Zero) { oobVector = Vector2.Normalize(-Position) * oobVectorWeight; } } var steeringVector = new Vector2(MathF.Cos(Angle), MathF.Sin(Angle)) + World.Hook.FlockWeight * flockingVector + oobVector; Angle = MathF.Atan2(steeringVector.Y, steeringVector.X); }
// Start is called before the first frame update void Start() { f.character = character; c.character = character; s.character = character; c.groupMates = flockMates; s.targets = flockMates; f.target = Leader; myMoveType = new Flocking(); myMoveType.character = character; myMoveType.flockmates = flockMates; myMoveType.weights[0] = weights[0]; myMoveType.weights[1] = weights[1]; myMoveType.weights[2] = weights[2]; myMoveType.behaviors[0] = f; myMoveType.behaviors[1] = c; myMoveType.behaviors[2] = s; myRotateType = new LookWhereGoing(); myRotateType.character = character; myRotateType.target = myTarget; }
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; }
protected virtual void Start() { this.Key = Flocking.RegisterParticle(this); // Choose random starting position velocity and accel this.Position = Camera.main.ScreenToWorldPoint(new Vector2(Camera.main.pixelWidth * Random.value, Camera.main.pixelHeight * Random.value)); Velocity = Vector2.ClampMagnitude(new Vector2(Random.value, Random.value), Flocking.MaxVelocity); this.transform.position = Position; }
void Awake() { _lineOfSight = GetComponent <LineOfSight>(); //_squirrel = (Squirrel3D)FindObjectOfType(typeof(Squirrel3D)); _lineOfSight.Target = _squirrel; _flocking = GetComponent <Flocking>(); _currentState = BeetleStates.Patrolling; _currentWaypoint = startPath; _flocking.Target = _currentWaypoint.transform.position; }
void OnTriggerEnter2D(Collider2D other) { if (other.gameObject.tag.Equals("Fly")) { Destroy(other.gameObject.GetComponent <Flocking>()); Flocking.DestroyFlockMember(other.gameObject); Destroy(other.gameObject); PlayerInfo.IncrementScore(); } }
public override void UpdateParticle() { base.UpdateParticle(); // Do some flocking Vector2 velSum = Vector2.zero; Vector2 posSum = Vector2.zero; Vector2 sepSum = Vector2.zero; int count = 0; foreach (var other in Flocking.InRadius(this, Flocking.AroundRadius)) { velSum += other.Velocity; posSum += other.Position; sepSum += (Position - other.Position) / (0.001f + Vector2.SqrMagnitude(other.Position - this.Position)); count++; } Vector2 alignment = Vector2.zero; Vector2 cohesion = Vector2.zero; Vector2 seperation = Vector2.zero; if (count > 0) { alignment = velSum / count; alignment = alignment - Velocity; alignment = Vector2.ClampMagnitude(alignment, Flocking.MaxAlignForce); cohesion = posSum / count; cohesion -= Position; cohesion = Vector2.ClampMagnitude(cohesion, Flocking.MaxVelocity); cohesion = Velocity - cohesion; cohesion = Vector2.ClampMagnitude(cohesion, Flocking.MaxCohesionForce); seperation = sepSum / count; seperation = Vector2.ClampMagnitude(seperation, Flocking.MaxSeperationForce); } Vector2 accel = alignment + cohesion + seperation; Velocity = Vector2.ClampMagnitude(Velocity + accel, Flocking.MaxVelocity); Position += Velocity; BounceEdge(); }
// Use this for initialization void Start() { boidsarray = new Flocking[number_of_boids]; //boidsvelocity = new Vector3[number_of_boids]; if (!stationaryCenter) { center = Vector3.zero; } else { center = transform.position; } int layer = landMesh.gameObject.layer; int layerMask = 1 << layer; for (int i = 0; i < number_of_boids; i++) { Flocking b = Instantiate(boid, new Vector3(Random.value, Random.value, Random.value), Quaternion.identity) as Flocking; b.transform.parent = transform; b.transform.localScale = Vector3.one; b.landLayerMask = layerMask; boidsarray[i] = b; b.velocity = flockInitialVelocity; b.wantsToLand = true; } if (highlightLeader) { GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere); sphere.transform.parent = boidsarray[0].transform; sphere.transform.localScale = Vector3.one * 5.0f; sphere.transform.position = sphere.transform.parent.position; sphere.renderer.material.color = Color.red; } flockInitialVelocity = Vector3.Normalize(flockInitialVelocity); if (flockHasLeader) { flockLeaderVelocity = flockInitialVelocity; } }
public override void UpdateParticle() { base.UpdateParticle(); Vector2 newPos = Position + Velocity; Particle nearest = Flocking.NearestParticle(this); if (nearest != null) { float coll = 0.5f; Vector2 myNormal = Velocity.normalized; Vector2 otherNormal = nearest.Velocity.normalized; Line myLine = new Line(Position, Position + myNormal * coll); Line otherLine = new Line(nearest.Position, nearest.Position + otherNormal * coll); if (!myLine.NoContactCertainty(otherLine) && !otherLine.NoContactCertainty(myLine)) { // Collision Vector2 myVel = Velocity; this.Velocity = Vector2.Reflect(myVel, new Vector2(-nearest.Velocity.y, nearest.Velocity.x)); nearest.Velocity = Vector2.Reflect(nearest.Velocity, new Vector2(-myVel.y, myVel.x)); //nearest.HandledAction = true; //this.HandledAction = true; this.spriteRenderer.color = Color.red; nearest.spriteRenderer.color = Color.red; } } //HandledAction = false; Velocity = Vector2.ClampMagnitude(Velocity, Flocking.MaxVelocity); Position += Velocity; BounceEdge(); }
void OnTriggerEnter2D(Collider2D other) { if (other.gameObject.tag.Equals("Fly")) { Flocking flocker = other.gameObject.GetComponent <Flocking>(); if (flocker != null) { Destroy(other.gameObject.GetComponent <Flocking>()); Flocking.DestroyFlockMember(other.gameObject); } Destroy(other.gameObject); transform.parent.gameObject.GetComponent <PlayerInfo>().IncrementScore(); if (survivalMode) { FlyPlayerInfo.DecrementFlyCount(); } } }
private float elapsedTime = 0f; // time elapsed since last generation // Update is called once per frame void Update() { if (generated == numInstances) { return; } GameObject clone; if (elapsedTime >= interval) { // spawn creating an instance... clone = Instantiate(sample); clone.transform.position = this.transform.position; KinematicState ks = clone.GetComponent <KinematicState> (); if (ks != null) { ks.maxSpeed = ks.maxSpeed + Utils.binomial() * variationRatio * ks.maxSpeed; ks.maxAcceleration = ks.maxAcceleration + Utils.binomial() * variationRatio * ks.maxAcceleration; } Flocking fk = clone.GetComponent <Flocking> (); if (fk != null) { fk.cohesionThreshold += Utils.binomial() * variationRatio * fk.cohesionThreshold; fk.repulsionThreshold += Utils.binomial() * variationRatio * fk.repulsionThreshold; fk.wanderRate += Utils.binomial() * variationRatio * fk.wanderRate; } generated++; elapsedTime = 0; } else { elapsedTime += Time.deltaTime; } }
private void FoundChurch() { GameObject follower; Vector3 rand; Vector3 speed; FlockingLeader jesus = leader.GetComponent <FlockingLeader>(); christians = new Flocking[boidsAmmount]; for (int i = 0; i < boidsAmmount; i++) { rand = SpawnPoint + Random.insideUnitSphere * 30; follower = Instantiate(followersPrefab); follower.transform.position = rand; speed = new Vector3(Random.Range(0, maxSpeed), Random.Range(0, maxSpeed), Random.Range(0, maxSpeed)); speed = Flocking.ClampSpeed(speed, minSpeed, maxSpeed); Flocking jose = follower.GetComponent <Flocking>(); jose.Set(speed, jesus, minSpeed, maxSpeed); christians[i] = jose; } speed = new Vector3(Random.Range(0, maxSpeed), Random.Range(0, maxSpeed), Random.Range(0, maxSpeed)); speed = Flocking.ClampSpeed(speed, minSpeed, maxSpeed); jesus.Set(christians, speed, minSpeed, maxSpeed); }
public override Vector2 CalculateMovement(Agent agent, List <Transform> objectsAround, Flocking flock) { // if no neighbors, no fix needed, return zero if (objectsAround.Count == 0) { return(Vector2.zero); } // add all point, calculate the average point between them //var avoidanceMovement = objectsAround.Aggregate( // Vector2.zero, // (current, t) => current + (Vector2)t.transform.up //); var avoidanceMovement = Vector2.zero; var nAvoid = 0; List <Transform> filterContext = (filter == null) ? objectsAround : filter.filter(agent, objectsAround); for (var i = 0; i < filterContext.Count; i++) { if (Vector2.SqrMagnitude(filterContext[i].position - agent.transform.position) < flock.SquareDistance) { nAvoid++; avoidanceMovement += (Vector2)(agent.transform.position - filterContext[i].position); } } if (nAvoid >= 0) { avoidanceMovement /= nAvoid; } return(avoidanceMovement); }
public override Vector2 CalculateMove(FlockingObejct flockingobejct, List <Transform> context, Flocking flock) { if (context.Count == 0) { return(flockingobejct.transform.up); } Vector2 alignment = Vector2.zero; List <Transform> filterContext = (filter == null) ? context : filter.FilterList(flockingobejct, context); foreach (Transform item in filterContext) { alignment += (Vector2)item.transform.up; } alignment /= context.Count; return(alignment); }
private float DistanceTo(Flocking orca) { return(Vector3.Distance(orca.transform.position, Position)); }
public override Vector2 CalculateMovement(Agent agent, List <Transform> objectsAround, Flocking flock) { // if no neighbors, no fix needed, maintain alignment if (objectsAround.Count == 0) { return(agent.transform.up); } // add all point, calculate the average point between them //var alignmentMovement = objectsAround.Aggregate( // Vector2.zero, // (current, t) => current + (Vector2)t.transform.up //); var alignmentMovement = Vector2.zero; List <Transform> filterContext = (filter == null) ? objectsAround : filter.filter(agent, objectsAround); for (var i = 0; i < filterContext.Count; i++) { alignmentMovement += (Vector2)filterContext[i].transform.up; } alignmentMovement /= filterContext.Count; return(alignmentMovement); }
public void Init(Flocking flocking) { objectFlocking = flocking; }
public override Vector2 CalculateMovement(Agent agent, List <Transform> objectsAround, Flocking flock) { // if no neighbors, no fix needed, return zero if (objectsAround.Count == 0) { return(Vector2.zero); } // add all point, calculate the average point between them //var cohesionMovement = objectsAround.Aggregate( // Vector2.zero, // (current, t) => current + (Vector2) t.position //); var cohesionMovement = Vector2.zero; List <Transform> filterContext = (filter == null) ? objectsAround : filter.filter(agent, objectsAround); for (var i = 0; i < filterContext.Count; i++) { cohesionMovement += (Vector2)filterContext[i].position; } cohesionMovement /= objectsAround.Count; // create offset from position cohesionMovement -= (Vector2)agent.transform.position; cohesionMovement = Vector2.SmoothDamp( agent.transform.up, cohesionMovement, ref currentVelocity, smoothTimeMovement ); return(cohesionMovement); }
//Using an abstract class because i don't want to instansiate any behaviour in this script public abstract Vector2 MovementCalculation(FlockingAgent agent, List <Transform> Neighbours, Flocking Flock);
private float DistanceTo(Flocking boid) { return(Vector3.Distance(boid.transform.position, Position)); }
// Use this for initialization void Start() { mainShip = GameObject.FindGameObjectWithTag("Player").GetComponent<Flocking>(); }
public void Init(Flocking flocking) { agentFlocking = flocking; }
public override Vector2 CalculateMove(FlockingObejct flockingobejct, List <Transform> context, Flocking flock) { Vector2 centerOffset = center - (Vector2)flockingobejct.transform.position; float i = centerOffset.magnitude / radius; if (i < 0.9f) { return(Vector2.zero); } return(centerOffset * i * i); }
public override Vector2 CalculateMove(FlockingObejct flockingobejct, List <Transform> context, Flocking flock) { if (context.Count == 0) { return(Vector2.zero); } Vector2 cohesion = Vector2.zero; List <Transform> filterContext = (filter == null) ? context : filter.FilterList(flockingobejct, context); foreach (Transform item in filterContext) { cohesion += (Vector2)item.position; } cohesion /= context.Count; cohesion -= (Vector2)flockingobejct.transform.position; return(cohesion); }
public override Vector2 CalculateMove(FlockingObejct flockingobejct, List <Transform> context, Flocking flock) { if (weights.Length != behaviors.Length) { return(Vector2.zero); } Vector2 move = Vector2.zero; for (int i = 0; i < behaviors.Length; i++) { Vector2 partMove = behaviors[i].CalculateMove(flockingobejct, context, flock) * weights[i]; if (partMove != Vector2.zero) { if (partMove.sqrMagnitude > weights[i] * weights[i]) { partMove.Normalize(); partMove *= weights[i]; } move += partMove; } } return(move); }
public abstract Vector2 CalculateMove(FlockingObejct flockingobject, List <Transform> context, Flocking flock);
public override Vector2 CalculateMovement(Agent agent, List <Transform> objectsAround, Flocking flock) { if (behavious.Length != scalars.Length) { Debug.LogError($"Data not the same size {name}", this); return(Vector2.zero); } Vector2 move = Vector2.zero; for (int i = 0; i < behavious.Length; i++) { Vector2 partialMove = behavious[i].CalculateMovement(agent, objectsAround, flock) * scalars[i]; if (partialMove != Vector2.zero) { if (partialMove.sqrMagnitude > scalars[i] * scalars[i]) { partialMove.Normalize(); partialMove *= scalars[i]; } move += partialMove; } } return(move); }