private void Update() { SteeringBehavior.MaxSpeed = MaxSpeed; SteeringBehavior.MaxVelocity = MaxVel; if (Input.GetMouseButton(0)) { Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition); // Debug.LogError("mousePos" + mousePos); mousePos.z = 0; Target = mousePos; } Vector2 sourcePos = new Vector2(transform.position.x, transform.position.y); Steer = SteeringBehavior.Arrive(sourcePos, Target, Velocity); // Debug.LogError("st->" + Steer.ToString()); Velocity = Steer * Time.deltaTime; Truncate(ref Velocity, MaxVel); if (Velocity.sqrMagnitude > MinVelocity) { transform.position += Velocity; Rotate(); } }
public Vector2 Follow(Vector2 sourcePos, Vector2 sourceVel, Vector2 leaderPos, Vector2 leaderVelocity) { Vector2 targetVelocity = leaderVelocity * DirectionFromLeader; targetVelocity = targetVelocity.normalized; targetVelocity *= LeaderBehindDistance; Vector2 behind = leaderPos + targetVelocity; return(Steering.Arrive(sourcePos, behind, sourceVel)); }
public override void FixedExecute() { //if the cell have not reach the teleport starting position, continue charge forward. If the cell //reach the teleport starting position, start the teleport corountine. The coroutine will disable the //enemy child cell for 1.5 second become teleport the enemy child cell to the next position and then //move towards the player main cell Vector2 Acceleration = Vector2.zero; if (m_bSqueezeDone && m_bTeleporting && !HasCellReachTargetPos(m_StartTelePos)) { Acceleration += SteeringBehavior.Seek(m_Child, m_StartTelePos, 45f); if (m_Child.transform.localScale.y < 1f && m_Child.transform.localScale.x > 0.5f) { m_Child.transform.localScale += m_ShrinkRate; } } else if (m_bSqueezeDone && m_bTeleporting && HasCellReachTargetPos(m_StartTelePos)) { m_bTeleported = true; m_bTeleporting = false; m_ecFSM.StartChildCorountine(Teleport()); } //If the cell has not reached the current target position, continue seek towards that target position and remain seperate from rest of the enemy child cells else if (m_bSqueezeDone && !HasCellReachTargetPos(m_CurrentTargetPoint.Position) && !m_bReachTarget) { Acceleration += SteeringBehavior.Arrive(m_Child, m_CurrentTargetPoint.Position, 0.03f); Acceleration += SteeringBehavior.Seperation(m_Child, TagNeighbours()) * 30f; if (m_Child.transform.localScale.y < 1f && m_Child.transform.localScale.x > 0.5f) { m_Child.transform.localScale += m_ShrinkRate; } } else if (m_bSqueezeDone && m_nCurrentTargetIndex + 1 < m_PathToTarget.Count && !m_bReachTarget) { m_nCurrentTargetIndex++; m_CurrentTargetPoint = m_PathToTarget[m_nCurrentTargetIndex]; } //If the cell had reached the teleporting position and it hasn't teleported, start teleporting the cell to the calculated position else if (m_bSqueezeDone && HasCellReachTargetPos(m_PathToTarget[m_PathToTarget.Count - 1].Position) && m_bTeleporting == false && m_bTeleported == false) { m_bTeleporting = true; } //Clamp the acceleration of the enemy child cell to a maximum value and then add that acceleration force to the enemy child cell Acceleration = Vector2.ClampMagnitude(Acceleration, m_fMaxAcceleration); m_ecFSM.rigidbody2D.AddForce(Acceleration, ForceMode2D.Force); //Rotate the enemy child cell based on the direction of travel m_ecFSM.RotateToHeading(); }
/// <summary> /// Depending on the phase the demo is in, have the agent do the appropriate steering. /// /// </summary> void FixedUpdate() { switch (mapState) { case 0: if (label) { // replace "First algorithm" with the name of the actual algorithm you're demoing // do this for each phase label.text = name.Replace("(Clone)", "") + "\nAt Rest"; } linear = Vector3.zero; angular = 0f; break; case 1: //dynamic seek if (label) { // replace "First algorithm" with the name of the actual algorithm you're demoing // do this for each phase label.text = name.Replace("(Clone)", "") + "\nAlgorithm: Dynamic Seek"; } stopped = false; ai.SetTarget(target); Debug.Log(gameObject.name + " target " + target.gameObject.name); linear = ai.Seek().linear; angular = ai.Seek().angular; break; case 2: //dynamic flee if (label) { label.text = name.Replace("(Clone)", "") + "\nAlgorithm: Dynamic Flee"; } stopped = false; linear = ai.Flee().linear; angular = ai.Flee().angular; break; case 3: // if (label) { label.text = name.Replace("(Clone)", "") + "\nAlgorithm: Pursue with Arrive"; } stopped = false; //persue with arrive linear = ai.Pursue().linear; angular = ai.Pursue().angular; // linear = ai.whatever(); -- replace with the desired calls // angular = ai.whatever(); break; case 4: if (label) { label.text = name.Replace("(Clone)", "") + "\nAlgorithm: Dynamic Evade"; } stopped = false; ai.SetTarget(target); //velocity = ai.PursueArrive(); linear = ai.Evade().linear; angular = ai.Evade().angular; break; case 5: //SEEK with FACE if (label) { label.text = name.Replace("(Clone)", "") + "\nAlgorithm: Dynamic Align"; } stopped = false; ai.SetTarget(target); linear = ai.Seek().linear; angular = ai.Face().angular; break; case 6: //JUST PLAIN FACE if (label) { label.text = name.Replace("(Clone)", "") + "\nAlgorithm: Dynamic Face"; } stopped = false; ai.SetTarget(target); linear = ai.Face().linear; angular = ai.Face().angular; break; case 7: if (label) { label.text = name.Replace("(Clone)", "") + "\nAlgorithm: Dynamic Wander"; } stopped = false; //rotation = ai.Face(rotation, linear); ai.SetTarget(target); linear = ai.Wander().linear; angular = ai.Wander().angular; break; case 8: if (label) { label.text = name.Replace("(Clone)", "") + "\nAlgorithm: Dynamic Wander"; } stopped = false; //rotation = ai.Face(rotation, linear); ai.SetTarget(target); linear = ai.Pursue().linear; angular = ai.Face().angular; break; case 9: if (label) { label.text = name.Replace("(Clone)", "") + "\nAlgorithm: Dynamic Wander"; } stopped = false; //rotation = ai.Face(rotation, linear); ai.SetTarget(target); linear = ai.Evade().linear; angular = ai.Face().angular; break; case 10: if (label) { label.text = name.Replace("(Clone)", "") + "\nAlgorithm: Dynamic Wander"; } stopped = false; //rotation = ai.Face(rotation, linear); ai.SetTarget(target); linear = ai.Arrive().linear; angular = ai.Arrive().angular; break; // ADD CASES AS NEEDED } UpdateMovement(linear, angular, Time.deltaTime); if (label) { label.transform.position = Camera.main.WorldToScreenPoint(this.transform.position); } }
/// <summary> /// Depending on the phase the demo is in, have the agent do the appropriate steering. /// /// </summary> void FixedUpdate() { switch (mapState) { case 0: linear = ai.Seek(); if (label) { // replace "First algorithm" with the name of the actual algorithm you're demoing // do this for each phase label.text = name.Replace("(Clone)", "") + "\nAlgorithm: Seek algorithm"; } break; case 1: linear = ai.Flee(); if (label) { // replace "First algorithm" with the name of the actual algorithm you're demoing // do this for each phase label.text = name.Replace("(Clone)", "") + "\nAlgorithm: Flee algorithm"; } // linear = ai.Pursue(); // For example // angular = ai.Face(); // For example // linear = ai.whatever(); -- replace with the desired calls // angular = ai.whatever(); break; case 2: linear = ai.Arrive(); if (label) { label.text = name.Replace("(Clone)", "") + "\nAlgorithm: Arrive algorithm"; } // linear = ai.whatever(); -- replace with the desired calls // angular = ai.whatever(); break; case 3: linear = ai.Persue(); if (label) { label.text = name.Replace("(Clone)", "") + "\nAlgorithm: Pursue algorithm"; } // linear = ai.whatever(); -- replace with the desired calls // angular = ai.whatever(); break; case 4: linear = ai.Evade(); if (label) { label.text = name.Replace("(Clone)", "") + "\nAlgorithm: Evade algorithm"; } // linear = ai.whatever(); -- replace with the desired calls // angular = ai.whatever(); break; case 5: angular = ai.Align(); if (label) { label.text = name.Replace("(Clone)", "") + "\nAlgorithm: Align algorithm"; } // linear = ai.whatever(); -- replace with the desired calls // angular = ai.whatever(); break; case 6: linear = ai.Arrive(); angular = ai.Align(); if (label) { label.text = name.Replace("(Clone)", "") + "\nAlgorithm: Align and Arrive"; } break; case 7: angular = ai.Face(); if (label) { label.text = name.Replace("(Clone)", "") + "\nAlgorithm: Face"; } break; case 8: angular = ai.Face(); linear = ai.Arrive(); if (label) { label.text = name.Replace("(Clone)", "") + "\nAlgorithm: Face and Arrive"; } break; // ADD CASES AS NEEDED case 20: return; } UpdateMovement(linear, angular, Time.deltaTime); DrawCircle(linear, 1); if (label) { label.transform.position = Camera.main.WorldToScreenPoint(this.transform.position); } }
/// <summary> /// Depending on the phase the demo is in, have the agent do the appropriate steering. /// /// </summary> void FixedUpdate() { switch (phase) { case 0: if (label) { label.text = tag; } break; case 1: if (label) { label.text = "Wander"; } (linear, angular) = Algo("Wander"); break; case 2: if (label) { label.text = "Evade"; } (linear, angular) = Algo("Evade"); //wolf if (ai.DistanceToTarget() < 2f) { linear = new Vector3(0f, 0f, 0f); ai.Stop(); label.text = "Collision!"; // Destroy(self); Invoke("DisappearWolf", 3); Invoke("DisappearHunter", 3); return; } break; case 3: if (label) { label.text = "Pursue"; } (linear, angular) = Algo("Pursue"); //if (ai.target.tag == "Red(Clone)" && ai.DistanceToTarget() < 2f) //{ // //phase = 5; //} if (ai.tag == "Wolf") { if (ai.DistanceToTarget() < 1.5f) { //linear = new Vector3(0, 0, 0); //angular = 0; if (timeCounter > 50) //1second { ai.Stop(); timeCounter -= 1; } else if (timeCounter <= 0) { (linear, angular) = Algo("Pursue"); timeCounter = 150; } else { ai.target = ai.house; //phase = 7;//gotohouse timeCounter -= 1; //timeCounter = 150; } } } //must be wolf, already meet red, target now is house if (ai.target.tag != "Red" && ai.DistanceToTarget() < 5f) { //Debug.Log(ai.tag+"?????????????????????????/"); phase = 5; } break; case 4: if (label) { label.text = "Wander"; } (linear, angular) = Algo("Wander"); if (ai.DistanceToTarget() < 20f) { if (ai.tag == "Wolf") { phase = 2; } else { phase = 3; } } break; case 5: if (label) { label.text = "Arrive"; } linear = ai.Arrive(); angular = ai.Face(); Debug.Log(ai.tag + " " + ai.distanceToHouse()); if (ai.ArriveHouse()) { phase = 9; } break; case 6: if (label) { label.text = "PathFollowWithAvoid"; } (linear, angular) = Algo("PathFollow"); if (GameObject.FindGameObjectsWithTag("Wolf").Length >= 1 && (ai.agent.position - GameObject.FindGameObjectsWithTag("Wolf")[0].GetComponent <NPCController>().position).magnitude < 1.5f) { if (timeCounter > 50) //1second { ai.Stop(); timeCounter -= 1; } else if (timeCounter <= 0) { (linear, angular) = Algo("PathFollow"); timeCounter = 150; } else { (linear, angular) = Algo("PathFollow"); timeCounter -= 1; } } break; case 7: if (label) { label.text = "gotohouse"; } linear = ai.Arrive(ai.target.position - ai.agent.position); angular = ai.Face(); Debug.Log(ai.tag + " " + ai.distanceToHouse()); if (ai.ArriveHouse()) { phase = 9; } break; case 8: if (label) { label.text = "SeekWithArrive"; } (linear, angular) = Algo("SeekWithArrive"); break; case 9: if (label) { label.text = ""; } if (ai.tag == "Wolf") { DisappearWolf(); GameObject.FindGameObjectWithTag("Narrator").GetComponent <Text>().text += "Wolf arrives!\n"; } else if (ai.tag == "Hunter") { DisappearHunter(); GameObject.FindGameObjectWithTag("Narrator").GetComponent <Text>().text += "Hunter arrives!\n"; } else { DisappearRed(); GameObject.FindGameObjectWithTag("Narrator").GetComponent <Text>().text += "Red arrives!\n"; } addEndingStory(); break; } update(linear, angular, Time.deltaTime); if (label) { label.transform.position = Camera.main.WorldToScreenPoint(this.transform.position); } }
/// <summary> /// Depending on the phase the demo is in, have the agent do the appropriate steering. /// /// </summary> void FixedUpdate() { switch (mapState) { case 0: label.text = ""; break; case 1: if (ai.GetComponent <SteeringBehavior>().tag == "Hunter") { linear = ai.Seek(); angular = ai.Face(); label.text = "1: dynamic seek + face"; } else { label.text = ""; } break; case 2: if (ai.GetComponent <SteeringBehavior>().tag == "Wolf") { linear = ai.Flee(); angular = ai.FaceAway(); label.text = "2:dynamic flee + faceaway"; } else { label.text = ""; } break; case 3: if (label) { label.text = "3: dynamic face "; } angular = ai.Face(); break; case 4: if (ai.tag == "Hunter") { label.text = "4: dynamic wander"; angular = ai.Wander(); linear = ai.maxSpeed * new Vector3(Mathf.Sin(ai.GetComponent <NPCController>().orientation), 0, Mathf.Cos(ai.GetComponent <NPCController>().orientation)); } else { label.text = ""; } //linear = ai.Seek(); //--replace with the desired calls //angular = ai.Wander(out linear); break; case 5: if (ai.tag == "Hunter") { linear = ai.Seek(); angular = ai.Face(); label.text = "5:dynamic seek + face"; } if (ai.tag == "Wolf") { linear = ai.Flee(); angular = ai.FaceAway(); label.text = "6:dynamic flee + faceaway"; } break; case 6: if (ai.tag == "Hunter") { label.text = "6: dynamic arrive + face"; linear = ai.Arrive(); angular = ai.Face(); } if (ai.tag == "Wolf") { label.text = "6:dynamic flee + faceaway"; linear = ai.Flee(); angular = ai.FaceAway(); } break; case 7: if (ai.tag == "Hunter") { label.text = "7: dynamic pursue + arrive"; Vector3 linear1 = ai.Arrive(); Vector3 linear2 = ai.Pursue(); if (linear1 != Vector3.zero) { linear = linear1 + linear2; } else { linear = linear1; } angular = ai.Face(); } if (ai.tag == "Wolf") { label.text = "7: dynamic wander"; angular = ai.Wander(); linear = linear = ai.maxSpeed * new Vector3(Mathf.Sin(ai.GetComponent <NPCController>().orientation), 0, Mathf.Cos(ai.GetComponent <NPCController>().orientation)); //linear = ai.Flee(); //linear = ai.Evade(); } break; case 8: if (ai.tag == "Hunter") { label.text = "8: dynamic pursue + face"; linear = ai.Pursue(); angular = ai.Face(); } if (ai.tag == "Wolf") { label.text = "8: dynamic evade + faceaway"; linear = ai.Evade(); angular = ai.FaceAway(); } break; case 9: if (ai.tag == "Hunter") { label.text = "9: dynamic align + pursue"; linear = ai.Pursue(); angular = ai.Align(); } if (ai.tag == "Wolf") { label.text = "9: dynamic wander"; angular = ai.Wander(); linear = ai.maxSpeed * new Vector3(Mathf.Sin(ai.GetComponent <NPCController>().orientation), 0, Mathf.Cos(ai.GetComponent <NPCController>().orientation)); } break; // ADD CASES AS NEEDED } UpdateMovement(linear, angular, Time.deltaTime); if (label) { label.transform.position = Camera.main.WorldToScreenPoint(this.transform.position); } }
/// <summary> /// Depending on the phase the demo is in, have the agent do the appropriate steering. /// /// </summary> void FixedUpdate() { switch (phase) { case 0: // stay still, used to delay movement linear = new Vector3(0, 0, 0); angular = 0; break; case 1: // Pursue // Assign algorithm label text if (label) { label.text = name.Replace("(Clone)", "") + "\nAlgorithm: Smarter Pursue"; } // Get linear acceleration linear = ai.Pursue(); linear = ai.WallAvoidance(linear, false, false); // Get angular accelleration angular = ai.Face(); break; case 2: // Arrive // Assign algorithm label text if (label) { label.text = name.Replace("(Clone)", "") + "\nAlgorithm: Smarter Arrive"; } // Get linear acceleration linear = ai.Arrive(); linear = ai.WallAvoidance(linear, false, false); // Get angular accelleration angular = ai.Face(); break; case 3: // Evade // Assign algorithm label text if (label) { label.text = name.Replace("(Clone)", "") + "\nAlgorithm: Smarter Evade"; } // Get linear acceleration linear = ai.Evade(); linear = ai.WallAvoidance(linear, false, false); // Get angular acceleration angular = ai.Face_Where_Im_Going(linear); break; case 4: if (label) { label.text = name.Replace("(Clone)", "") + "\nAlgorithm: Smarter Wander algorithm"; } angular = ai.Wander(out linear); //linear = ai.WallAvoidance(linear); //angular = ai.Face_Where_Im_Going(linear); break; case 5: if (label) { label.text = name.Replace("(Clone)", "") + "\nAlgorithm: Wall Avoidance algorithm"; } linear = ai.Pursue(); linear = ai.WallAvoidance(linear, false, false); angular = ai.Face_Where_Im_Going(linear); // linear = ai.whatever(); -- replace with the desired calls // angular = ai.whatever(); break; case 6: if (label) { label.text = name.Replace("(Clone)", "") + "\nAlgorithm: Collision Detection algorithm"; } linear = ai.Pursue(); linear = ai.WallAvoidance(linear, true, false); angular = ai.Face_Where_Im_Going(linear); // linear = ai.whatever(); -- replace with the desired calls // angular = ai.whatever(); break; case 7: if (label) { label.text = name.Replace("(Clone)", "") + "\nAlgorithm: Collision Prediction algorithm"; } linear = ai.Pursue(); linear = ai.WallAvoidance(linear, false, true); angular = ai.Face_Where_Im_Going(linear); // linear = ai.whatever(); -- replace with the desired calls // angular = ai.whatever(); break; case 8: if (label) { label.text = name.Replace("(Clone)", "") + "\nAlgorithm: Chase the player algorithm"; } linear = ai.ChasePlayer(); linear = ai.WallAvoidance(linear, false, false); angular = ai.Face_Where_Im_Going(linear); // linear = ai.whatever(); -- replace with the desired calls // angular = ai.whatever(); break; case 9: if (label) { label.text = name.Replace("(Clone)", "") + "\nAlgorithm: Follow the path algorithm"; } linear = ai.PathFollow(); angular = ai.Face_Where_Im_Going(linear); break; } update(linear, angular, Time.deltaTime); if (label) { label.transform.position = Camera.main.WorldToScreenPoint(this.transform.position); } }
/// <summary> /// Depending on the phase the demo is in, have the agent do the appropriate steering. /// /// </summary> /// jessie and patrick void FixedUpdate() { switch (mapState) { case 0: if (label) { label.text = name.Replace("(Clone)", "") + "\nWandering..."; } linear = ai.Wander(out angular); DrawCircle(ai.wanderCircleCenter, ai.wanderRadius); break; case 1: if (label) { // replace "First algorithm" with the name of the actual algorithm you're demoing // do this for each phase label.text = name.Replace("(Clone)", "") + "\nAlgorithm: dynamic evade algorithm"; } linear = ai.Evade(); // linear = ai.Pursue(); // For example // angular = ai.Face(); // For example // linear = ai.whatever(); -- replace with the desired calls // angular = ai.whatever(); break; case 2: if (label) { label.text = name.Replace("(Clone)", "") + "\nAlgorithm: Pursue algorithm"; } linear = ai.Pursue(); angular = ai.Face(); break; case 3: if (label) { label.text = name.Replace("(Clone)", "") + "\nAlgorithm: dyanamic Seek"; } linear = ai.Seek(); angular = ai.Face(); break; case 4: if (label) { label.text = name.Replace("(Clone)", "") + "\nAlgorithm: Flee algorithm"; } linear = ai.Flee(); //-- replace with the desired calls // angular = ai.whatever(); break; case 5: if (label) { label.text = name.Replace("(Clone)", "") + "\nAlgorithm: Spinning"; } // linear = ai.whatever(); -- replace with the desired calls angular = 0.1f; break; // ADD CASES AS NEEDED case 6: if (label) { label.text = name.Replace("(Clone)", "") + "\nAlgorithm: dynamic align"; } angular = ai.Align(); break; case 7: if (label) { label.text = name.Replace("(Clone)", "") + "\nAlgorithm: dynamic arrive"; } linear = ai.Arrive(); DrawConcentricCircle(ai.slowRadiusL); break; case 8: if (label) { label.text = name.Replace("(Clone)", "") + "\nAlgorithm: dynamic face"; } angular = ai.Face(); break; } UpdateMovement(linear, angular, Time.deltaTime); if (label) { label.transform.position = Camera.main.WorldToScreenPoint(this.transform.position); } }