/// <summary> /// Moves every flock agent by the calculated amount each frame /// </summary> void Update() { foreach (FlockAgent agent in GameManager.Get().agents) { if (agent != null) { string type = agent.getType(); if (type == "Witch") { maxSpeed = 1.5f; } else if (type == "Vampire") { maxSpeed = 1.5f; } else { maxSpeed = 5; } maxSpeed *= baseSpeedFactor; squareMaxSpeed = maxSpeed * maxSpeed; List <Transform> context = GetNearbyObjects(agent); Vector3 move = behaviour.CalculateMove(agent, context, this); move *= baseSpeedFactor; if (move.sqrMagnitude > squareMaxSpeed) { move = move.normalized * maxSpeed; /// make it one then times by max speed } agent.Move(move); } } }
void Update() { foreach (FlockAgent agent in agents) { // Create a list called context, this will deal with things in context to our neighbour radius // This list looks at nearby objects using the agent we are currently looking at List <Transform> context = GetNearbyObjects(agent); // For demo, checks how many neighbours the agent has, the more neighbours, the greener the agent will appear (from white) agent.GetComponentInChildren <SpriteRenderer>().color = Color.Lerp(Color.white, Color.green, context.Count / 6f); // Use the nearby objects - takes in our agent, the list of neighbours and the flock (this) as parameters // This returns back the way in which the agent should move Vector2 move = behaviour.CalculateMove(agent, context, this); // Speed the agent up move *= driveFactor; // Then check we have not exceeded our maxSpeed limit // If it has, bring speed back to the maximum if (move.sqrMagnitude > squareMaxSpeed) { // This resets magnitude to 1 then multiplies it's current speed to our maxSpeed move = move.normalized * maxSpeed; } agent.Move(move); } }
// Update is called once per frame void Update() { foreach (FlockAgent agent in agents) { if (agent.IsAlive() != false) { List <Transform> context = GetNearbyObjects(agent); //For testing //agent.GetComponentInChildren<SpriteRenderer>().color = Color.Lerp(Color.white, Color.red, context.Count / 6f); Vector2 move = behaviour.CalculateMove(agent, context, this); move *= driveFactor; if (move.sqrMagnitude > squareMaxSpeed) { move = move.normalized * maxSpeed; } agent.Move(move); if (Vector2.Distance(agent.rbSheepDog.position, agent.rbSheep.position) < 2f) { Vector2 direction = (agent.rbSheep.position - agent.rbSheepDog.position).normalized; Vector2 moveAvoidSheepDog = direction * maxSpeed; agent.Move(moveAvoidSheepDog); } } } }
// Update is called once per frame void Update() { foreach (FlockAgent agent in Agents) //making the flock move around and behave { List <Transform> context = GetNearbyObjects(agent); Vector3 move = Behaviour.CalculateMove(agent, context, this); move *= DriveFactor; if (move.sqrMagnitude > SquareMaxSpeed) { move = move.normalized * MaxSpeed; } agent.Move(move); } }
void Update() { foreach (FlockAgent agent in agents) { List <Transform> context = GetNearbyObjects(agent); Vector2 move = behaviour.CalculateMove(agent, context, this); move *= driveFactor; if (move.sqrMagnitude > sqrMaxSpeed) { move = move.normalized * maxSpeed; } agent.Move(move); } }
// Update is called once per frame void Update() { foreach (FlockAgent agent in agents) { List <Transform> context = GetNearbyObjects(agent); //agent.GetComponentInChildren<Renderer>().material.color = Color.Lerp(Color.white, Color.red, context.Count / 6f); Vector3 move = behaviour.CalculateMove(agent, context, this); move *= driveFactor; if (move.sqrMagnitude > squareMaxSpeed) { move = move.normalized * maxSpeed; } agent.move(move); } }
// Update is called once per frame void Update() { if (GameManager.Instance.paused) { return; } //target = player.transform.position; Vector3 move = Vector3.zero; float shipSpeed = FlockLeader.Stats.shipSpeed; float rotationSpeed = FlockLeader.Stats.rotationSpeed; if (FlockLeader.CurrentStateID == FSMStateID.Attacking) { shipSpeed = FlockLeader.Stats.attackShipSpeed; rotationSpeed = FlockLeader.Stats.attackRotationSpeed; } //Loop through each agent and run the behaviours foreach (FlockAgent agent in agents) { if (FlockLeader.CurrentStateID == FSMStateID.Spawned) { move = flockLeader.transform.forward; } else { List <Transform> context = new List <Transform>(); List <Transform> obstacles = new List <Transform>(); context = GetNearbyNeighbours(agent, context); //Use the physics engine to get nearby agents obstacles = GetNearbyObstacles(agent, obstacles); move = behaviour.CalculateMove(agent, context, this, obstacles); //Move the agent with the behaviour object } agent.Move(move, shipSpeed, rotationSpeed); //Move agent } //Debug.Log("Movement: " + move); if (agents.Count == 0) { Destroy(gameObject); } }
// Update is called once per frame void Update() { //Iterate through agents foreach (FlockAgent agent in agents) { //What is inside the agent radius List <Transform> contex = GetNearbyObjects(agent); Vector2 move = behaviour.CalculateMove(agent, contex, this); move *= driveFactor; if (move.sqrMagnitude > squareMaxSpeed) { move = move.normalized * maxSpeed; } agent.Move(move); } }
private void Update() { foreach (FlockAgent agent in agents) { List <Transform> context = GetNearbyObjects(agent, neighbourRadius); List <Transform> areaContext = GetNearbyObjects(agent, areaRadius); //TEST ONLY!! //agent.GetComponent<SpriteRenderer>().color = Color.Lerp(Color.white, Color.red, context.Count / 6f); Vector2 move = behaviour.CalculateMove(agent, context, areaContext, this); move *= driveFactor; if (move.sqrMagnitude > squareMaxSpeed) { move = move.normalized * maxSpeed; } agent.Move(move); } }
private void Update() { foreach (FlockAgent agent in agents) //For each FlockAgent within the list of all the agents (set up within the Start method) { #region Context Setup context = GetNearbyObjects(agent, neighbourRadius); //The context is the objects within the neighbourhood radius of the agent areaContext = GetNearbyObjects(agent, areaRadius); //The area context is the objects within the area radius of the agent #endregion #region Color Changing if (colorChangeState == ColorChangeState.Single) //If we want to use a single color { agent.GetComponent <SpriteRenderer>().color = startColor; //Set the agents color to be } else if (colorChangeState == ColorChangeState.Gradient) //If we want a gradient effect { agent.GetComponent <SpriteRenderer>().color = Color.Lerp(startColor, fadeToColor, context.Count / colorLerpDivider); //Fade between the two colors using the divider value as our time argument } #endregion #region Movement Vector2 move = behaviour.CalculateMove(agent, context, areaContext, this); //Calculate the agents movements for this frame and set move to its value move *= driveFactor; //Multiply the movements by the drive factor if (move.sqrMagnitude > squareMaxSpeed) //if the moves squared magnitude is greater than the squared max speed { if (randomiseSpeed) //if we want to randomise the speed { float tempSpeedRandomiser = Random.Range(maxSpeed / 4, maxSpeed / 2); //set the temp speed to a random speed move = move.normalized * (maxSpeed + tempSpeedRandomiser); //the move is set to a normalized value then mutliplied by the max speed plus our random speed } else //otherwise { move = move.normalized * maxSpeed; //the move is set to a normalized value then mutliplied by the max speed (no randomising) } } agent.Move(move); //apply the movement to the agent #endregion } }
void UpdateFlock(int firstAgent, int numberOfAgents) { // Update all behaviours of all agents of this flock for (int i = firstAgent; i < firstAgent + numberOfAgents; i++) { List <Transform> context = agents[i].GetNearbyObjects(); if (debug) { if (context.Count == 0) { agents[i].SetColor(Color.black); } else { agents[i].SetColor(Color.Lerp(Color.white, Color.red, context.Count / 10f)); } } agents[i].Move(behaviour.CalculateMove(agents[i], context, this), Time.deltaTime * updateGroups); } }
// Update is called once per frame void Update() { foreach (FlockAgent agent in agents) { if (!agent) { agents.Remove(agent); // Escaped cheeto gets destroyed and removed from list } else { List <Transform> context = GetNearbyObjects(agent); // What exists in the neighbour radius Vector3 move = behaviour.CalculateMove(agent, context, this); move *= driveFactor; if (move.sqrMagnitude > squareMaxSpeed) { move = move.normalized * maxSpeed; move = new Vector3(move.x, 0, move.z); } agent.Move(move); } } }
// Update is called once per frame void Update() { Vector3 target = GameObject.FindGameObjectWithTag("Player").transform.position; if (Vector3.Distance(transform.position, GameObject.FindGameObjectWithTag("Player").transform.position) < chasingDistance) { state = AIState.CHASING; } else { state = AIState.WANDERING; } foreach (FlockAgent agent in agents) { // IF IN WANDER STATE: switch (state) { case AIState.WANDERING: // Debug.Log("WANDERING!!!"); List <Transform> context = GetNearbyObjects(agent); agent.GetComponentInChildren <Animator>().SetBool("IsChasing", false); agent.GetComponentInChildren <Animator>().SetBool("IsAttacking", false); Vector3 move = behaviour.CalculateMove(agent, context, this); move *= driveFactor; move.y = 0; if (move.sqrMagnitude > squareMaxSpeed) { move = move.normalized * maxSpeed; } agent.Move(move); break; case AIState.CHASING: //agent.GetComponentInChildren<MeshRenderer>().material.color = Color.white; agent.GetComponentInChildren <Animator>().SetBool("IsChasing", true); Vector3 dir = (target - agent.transform.position).normalized; // agent.GetComponentInChildren<MeshRenderer>().material.color = Color.white; if (Vector3.Distance(target, agent.transform.position) <= agent.transform.GetComponent <SphereCollider>().radius) { state = AIState.ATTACKING; } else { agent.transform.rotation = Quaternion.LookRotation(dir, Vector3.up); agent.transform.position = agent.transform.position + dir * (maxSpeed / 2) * Time.deltaTime; } break; case AIState.ATTACKING: if (Vector3.Distance(target, agent.transform.position) <= agent.transform.GetComponent <SphereCollider>().radius) { // Debug.Log("attack attack attack!!!"); //agent.GetComponentInChildren<MeshRenderer>().material.color = Color.cyan; agent.GetComponentInChildren <Animator>().SetBool("IsAttacking", true); } else { state = AIState.CHASING; // agent.GetComponentInChildren<MeshRenderer>().material.color = Color.white; } break; } } }