public override Vector3 Execute() { Vector3 force = Vector3.zero; GameObject[] gameObjects = perception.GetGameObjects(); if (gameObjects != null && gameObjects.Length > 0) { // **** Vector3 velocities = Vector3.zero; foreach (GameObject gameObject in gameObjects) { AutonomousAgent agent = gameObject.GetComponent <AutonomousAgent>(); velocities = velocities + agent.Velocity; } Vector3 direction = (velocities / gameObjects.Length).normalized; // **** Vector3 desired = direction * Agent.maxSpeed; force = Vector3.ClampMagnitude(desired - Agent.Velocity, Agent.maxForce); Debug.DrawRay(transform.position, desired, Color.red); // desired Debug.DrawRay(transform.position + Agent.Velocity, force, Color.green); // steering } return(force); }
public override Vector3 Execute(AutonomousAgent agent, AutonomousAgent target, string targetTag) { Vector3 steering = Vector3.zero; //get all agent game objects within perception radius GameObject[] gameObjects = AutonomousAgent.GetGameObjects(gameObject, targetTag, perception); if (gameObjects.Length > 0) { //get center of all agents in perception Vector3 sum = Vector3.zero; foreach (GameObject gameObject in gameObjects) { AutonomousAgent targetAgent = (gameObject) ? target.GetComponent <AutonomousAgent>() : null; Vector3 direction = agent.position - targetAgent.position; float distance = direction.magnitude; direction = direction.normalized; direction = direction / distance; sum = sum + direction; } Vector3 desired = sum.normalized * agent.maxSpeed; steering = desired - agent.velocity; } return(steering); }
public override Vector3 ExecuteOrder66(AutonomousAgent agent, AutonomousAgent target, string targetTag = "") { Vector3 steering = Vector3.zero; //get all agent game objects within perception radius GameObject[] gameObjects = AutonomousAgent.GetGameObjects(gameObject, targetTag, perception); if(gameObjects.Length > 0) { //get center position of all agents within perception Vector3 sum = Vector3.zero; foreach(GameObject gameObject in gameObjects) { AutonomousAgent targetAgent = (gameObject) ? gameObject.GetComponent<AutonomousAgent>() : null; sum = sum + targetAgent.position; } sum = sum / gameObjects.Length; //seek to center position Vector3 desired = (sum - agent.position).normalized * agent.maxSpeed; steering = desired - agent.velocity; } return steering; }
void Update() { acceleration = Vector3.zero; //Apply force to acceleration for all behaviors foreach (AutonomousBehavior behavior in m_behaviors) { GameObject target = (behavior.targetTagName != "NONE") ? GetNearestGameObject(gameObject, behavior.targetTagName, behavior.perception) : null; AutonomousAgent targetAgent = (target) ? target.GetComponent <AutonomousAgent>() : null; Vector3 force = behavior.ExecuteOrder66(this, targetAgent, behavior.targetTagName); force = Vector3.ClampMagnitude(force, maxForce); force = force * behavior.strength; ApplyForce(force); } velocity = velocity + acceleration; velocity = Vector3.ClampMagnitude(velocity, maxSpeed); transform.position = transform.position + velocity * Time.deltaTime; transform.position = WrapPosition(transform.position, new Vector3(-10.0f, -10.0f, -10.0f), new Vector3(10.0f, 10.0f, 10.0f)); Quaternion targetRotation = Quaternion.LookRotation(forward, Vector3.up); transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, m_rotateRate * Time.deltaTime); //Debug.DrawLine(transform.position, transform.position + velocity, Color.white); }
void Update() { acceleration = Vector3.zero; foreach (Behaviors behavior in m_behaviors) { //float scale = 1.0f - Mathf.Clamp01(acceleration.magnitude / maxForce); GameObject target = (behavior.targetTagName != "NONE") ? GetNearestGameObject(gameObject, behavior.targetTagName, behavior.perception) : null; AutonomousAgent targetAgent = (target) ? target.GetComponent <AutonomousAgent>() : null; Vector3 force = behavior.Execute(this, targetAgent, behavior.targetTagName); force = Vector3.ClampMagnitude(force, maxForce); force.y = 0.0f; ApplyForce(force); } velocity += acceleration; //* Time.deltaTime; velocity = Vector3.ClampMagnitude(velocity, maxSpeed); transform.position += velocity * Time.deltaTime; transform.position = WrapPosition(transform.position, new Vector3(-10.0f, -10.0f, -10.0f), new Vector3(10.0f, 10.0f, 10.0f)); Quaternion targetRotation = Quaternion.LookRotation(forward, Vector3.up); transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, m_rotateRate * Time.deltaTime); Debug.DrawLine(transform.position, transform.position + velocity, Color.white); }
private void Awake() { owner = GetComponentInParent <AutonomousAgent>(); triggerCollider = GetComponent <SphereCollider>(); lastColliders = new List <Collider>(); StartCoroutine(CheckInside(Random.Range(0.1f, 0.5f))); }
protected AutonomousAgentInRole( Guid id, AutonomousAgent autonomousAgent, Role role ) : base(id) { AutonomousAgent = autonomousAgent; Role = role; }
private void Awake() { owner = GetComponentInParent<AutonomousAgent>(); triggerCollider = GetComponent<SphereCollider>(); lastColliders = new List<Collider>(); StartCoroutine(CheckInside(Random.Range(0.1f, 0.5f))); }
private void Start() { triggerCollider = GetComponent<SphereCollider>(); owner = GetComponentInParent<AutonomousAgent>(); opposingTeam = Targeting.GetOpposingTeam(owner.Team); lastColliders = new List<Collider>(); StartCoroutine(CheckInside(Random.Range(1f, 2f))); }
private void Start() { triggerCollider = GetComponent <SphereCollider>(); owner = GetComponentInParent <AutonomousAgent>(); opposingTeam = Targeting.GetOpposingTeam(owner.Team); lastColliders = new List <Collider>(); StartCoroutine(CheckInside(Random.Range(1f, 2f))); }
public override Vector3 ExecuteOrder66(AutonomousAgent agent, AutonomousAgent target, string targetTag = "") { m_theta = m_theta + Random.Range(-m_displacement, m_displacement); Vector3 randomPoint = Quaternion.AngleAxis(m_theta, Vector3.up) * new Vector3(0.0f, 0.0f, m_radius); Vector3 forward = agent.forward * m_forwardDistance; Vector3 direction = forward + randomPoint; Vector3 desired = direction.normalized * agent.maxSpeed; Vector3 steering = desired - agent.velocity; return(steering); }
public override Vector3 Execute(AutonomousAgent agent, AutonomousAgent target, string targetTag) { if (target == null) { return(Vector3.zero); } Vector3 desired = (agent.position - target.position).normalized * agent.maxSpeed; Vector3 steering = desired - agent.velocity; steering = Vector3.ClampMagnitude(steering, agent.maxForce); return(steering); }
public void LinkNodes() { GameObject[] gameObjects = GameObject.FindGameObjectsWithTag("Node"); foreach(GameObject go in gameObjects) { Node node = go.GetComponent<Node>(); if (node) { bool connected = false; Collider[] colliders = Physics.OverlapSphere(go.transform.position, m_nodeRange); foreach (Collider collider in colliders) { Node otherNode = collider.GetComponent<Node>(); if (otherNode && otherNode != node) { Node.Edge edge; edge.nodeA = node; edge.nodeB = otherNode; node.edges.Add(edge); connected = true; } } if (connected == false) { GameObject nearestGameObject = AutonomousAgent.GetNearestGameObject(node.gameObject, "Node"); Node otherNode = nearestGameObject.GetComponent<Node>(); if (otherNode) { Node.Edge edge; edge.nodeA = node; edge.nodeB = otherNode; node.edges.Add(edge); connected = true; } } } } }
public override Vector3 Execute(AutonomousAgent agent, AutonomousAgent target, string targetTag) { if (target == null) { return(Vector3.zero); } Vector3 desired = (target.position - agent.position); float distance = desired.magnitude; desired = desired.normalized * agent.maxSpeed; if (distance < m_radius) { desired = desired * (distance / m_radius); } Vector3 steering = desired - agent.velocity; //steering = Vector3.ClampMagnitude(steering, agent.maxForce); return(steering); }
public override Vector3 Execute(AutonomousAgent agent, AutonomousAgent target, string targetTag) { Vector3 steering = Vector3.zero; GameObject[] gameObjects = AutonomousAgent.GetGameObjects(gameObject, targetTag, perception); if (gameObjects.Length > 0) { Vector3 sum = Vector3.zero; foreach (GameObject gameObject in gameObjects) { AutonomousAgent targetAgent = (gameObject) ? gameObject.GetComponent <AutonomousAgent>() : null; sum = sum + targetAgent.position; } sum = sum / gameObjects.Length; Vector3 desired = (sum - agent.position).normalized * agent.maxSpeed; steering = desired - agent.velocity; } return(steering); }
public override Vector3 Execute(AutonomousAgent agent, AutonomousAgent target, string targetTag) { Vector3 steering = Vector3.zero; //get all agent game objects within perception radius GameObject[] gameObjects = AutonomousAgent.GetGameObjects(gameObject, targetTag, perception); if (gameObjects.Length > 0) { //get center of all agents in perception Vector3 sum = Vector3.zero; foreach (GameObject gameObject in gameObjects) { AutonomousAgent targetAgent = (gameObject) ? target.GetComponent <AutonomousAgent>() : null; sum = sum + targetAgent.velocity; } Vector3 averageVelocity = sum / gameObjects.Length; //allign to average velocity position Vector3 desired = averageVelocity.normalized * agent.maxSpeed; steering = desired - agent.velocity; } return(steering); }
private void Awake() { smartDriver = GameObject.FindWithTag("Agent").GetComponent <AutonomousAgent>(); }
public abstract Vector3 ExecuteOrder66(AutonomousAgent agent, AutonomousAgent target, string targetTag = "");
public SteeringBehaviour(AutonomousAgent value) { agent = value; }
private void Awake() { owner = GetComponentInParent <AutonomousAgent>(); opposingTeam = Targeting.GetOpposingTeam(owner.Team); }
private void Awake() { owner = GetComponentInParent<AutonomousAgent>(); opposingTeam = Targeting.GetOpposingTeam(owner.Team); }