public wanderSteering Flocking() { wanderSteering result = new wanderSteering(); Vector3 seperation = FlockCollisionDetectAndAvoid(); Vector3 cohesion = Cohesion(); wanderSteering align = AlignCloseBy(); result.angular = align.angular; result.linear = result.linear + seperation * seperationMultiplier + cohesion * cohesionMultiplier + align.linear * alignMultiplier; return(result); }
private wanderSteering AlignCloseBy() { wanderSteering result = new wanderSteering(); result.angular = 0f; result.linear = new Vector3(0f, 0f, 0f); if (GameObject.Find("PhaseManager") == null) { return(result); } else if (GameObject.Find("PhaseManager").GetComponent <FieldMapManager>() == null) { return(result); } //get all npcs in the scene List <GameObject> allNPCs = GameObject.Find("PhaseManager").GetComponent <FieldMapManager>().AllNPCs(); Vector3 averageVelocity = new Vector3(); int NPCCount = 0; foreach (GameObject NPC in allNPCs) { if (NPC.tag == this.tag && NPC != this) { NPCController nc = NPC.GetComponent <NPCController>(); //if close enough if ((nc.position - agent.position).magnitude < 5.0f) { averageVelocity += nc.velocity; NPCCount += 1; } } } averageVelocity /= NPCCount; result.angular = Align(averageVelocity + agent.position); result.linear = Seek(averageVelocity - agent.velocity + agent.position); return(result); }
public void FixedUpdate() { //ensure it is time to start path following and you havent reached the end if (current < Path.Length && startPathFollowing) { //get start and end points for section of path startPos = agent.transform; endPos = Path[current].transform; //create struct to hold output wanderSteering ret; ret.linear = Vector3.zero; ret.angular = 0f; if (partTwo == true) { if (coneCheck) { //perform cone check wanderSteering coneCheckRet = ConeCheck(); if (coneCheckRet.linear != Vector3.zero && Mathf.Abs(coneCheckRet.angular) > Mathf.Epsilon) { ret.linear = coneCheckRet.linear; ret.angular = coneCheckRet.angular; } else { //calculate angular direction ret.angular = Align(endPos.position); } } else { //calculate angular direction ret.angular = Align(endPos.position); } //check for collision detection otherwise keep linear normal if (collisionPrediction) { Vector3 foundCollision = CollisionDetectAndAvoid(); if (foundCollision == Vector3.zero) { ret.linear = maxAcceleration * new Vector3(Mathf.Sin(agent.orientation), 0, Mathf.Cos(agent.orientation)); } else { ret.linear = foundCollision; } } else { ret.linear = maxAcceleration * new Vector3(Mathf.Sin(agent.orientation), 0, Mathf.Cos(agent.orientation)); } } if (partThree == true) { ret.angular = Align(endPos.position); ret.linear = Seek(endPos.position); wallAvoidanceSteering wa = Wallavoidance(); if (wa.needAvoidance == true) { ret.angular += 0.7f * wa.angular; ret.linear += 0.7f * wa.linear; } wanderSteering flocking = Flocking(); if (flocking.linear == Vector3.zero && flocking.angular < Mathf.Epsilon) { ret.angular += 0.6f * flocking.angular; ret.linear += 0.6f * flocking.linear; } } agent.GetComponent <NPCController>().update(ret.linear, ret.angular, Time.deltaTime); //if you reached endPos advance to next point in path if ((agent.position - endPos.position).magnitude <= targetRadiusL) { Debug.Log("current is " + current); current += 1; } } /*//othewise dont move player * else if (current >= Path.Length) * { * agent.GetComponent<NPCController>().UpdateMovement(Vector3.zero, 0f, Time.deltaTime); * }*/ }