Esempio n. 1
0
    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);
    }
Esempio n. 2
0
    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);
    }
Esempio n. 3
0
    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;
    }
Esempio n. 4
0
    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);
    }
Esempio n. 5
0
    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);
    }
Esempio n. 6
0
    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;
 }
Esempio n. 8
0
    private void Awake()
    {
        owner = GetComponentInParent<AutonomousAgent>();

        triggerCollider = GetComponent<SphereCollider>();
        lastColliders = new List<Collider>();

        StartCoroutine(CheckInside(Random.Range(0.1f, 0.5f)));
    }
Esempio n. 9
0
    private void Start()
    {
        triggerCollider = GetComponent<SphereCollider>();
        owner = GetComponentInParent<AutonomousAgent>();
        opposingTeam = Targeting.GetOpposingTeam(owner.Team);

        lastColliders = new List<Collider>();
        StartCoroutine(CheckInside(Random.Range(1f, 2f)));
    }
Esempio n. 10
0
    private void Start()
    {
        triggerCollider = GetComponent <SphereCollider>();
        owner           = GetComponentInParent <AutonomousAgent>();
        opposingTeam    = Targeting.GetOpposingTeam(owner.Team);

        lastColliders = new List <Collider>();
        StartCoroutine(CheckInside(Random.Range(1f, 2f)));
    }
Esempio n. 11
0
    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);
    }
Esempio n. 12
0
    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);
    }
Esempio n. 13
0
    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;
                    }
                }
            }
        }
    }
Esempio n. 14
0
    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);
    }
Esempio n. 15
0
    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);
    }
Esempio n. 16
0
    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);
    }
Esempio n. 17
0
 private void Awake()
 {
     smartDriver = GameObject.FindWithTag("Agent").GetComponent <AutonomousAgent>();
 }
Esempio n. 18
0
 public abstract Vector3 ExecuteOrder66(AutonomousAgent agent, AutonomousAgent target, string targetTag = "");
Esempio n. 19
0
 public SteeringBehaviour(AutonomousAgent value)
 {
     agent = value;
 }
Esempio n. 20
0
 public SteeringBehaviour(AutonomousAgent value)
 {
     agent = value;
 }
Esempio n. 21
0
 private void Awake()
 {
     owner        = GetComponentInParent <AutonomousAgent>();
     opposingTeam = Targeting.GetOpposingTeam(owner.Team);
 }
Esempio n. 22
0
 private void Awake()
 {
     owner = GetComponentInParent<AutonomousAgent>();
     opposingTeam = Targeting.GetOpposingTeam(owner.Team);
 }