// Start is called before the first frame update
 void Start()
 {
     myHerdBox = GetComponentInChildren <OnlineHerdBox>();
     if (GetComponent <OnlinePlayerProperties>())
     {
         playerNum = GetComponent <OnlinePlayerProperties>().playerNum;
         myHerd    = GameObject.Find("Herd " + GetComponent <OnlinePlayerProperties>().teamColor).GetComponent <OnlineHerd>();
     }
     else if (GetComponent <ThirdPersonController>())
     {
         playerNum = GetComponent <ThirdPersonController>().PlayerNum;
     }
 }
Пример #2
0
 // Start is called before the first frame update
 void Start()
 {
     player  = gameObject.GetComponentInParent <OnlineHerdShepherd>();
     boxHerd = player.myHerd;
 }
Пример #3
0
    public override Vector3 CalculateMove(OnlineHerdAgent agent, List <Transform> context, OnlineHerd herd)
    {
        // if no neighbors return no adjustment
        if (context.Count == 0)
        {
            return(Vector3.zero);
        }

        // add all points together and average
        Vector3 avoidanceMove = Vector3.zero;
        int     nAvoid        = 0;

        List <Transform> filterContext = (filter == null) ? context : filter.Filter(agent, context); // this is a filtered behavior

        foreach (Transform item in filterContext)
        {
            if (Vector3.SqrMagnitude(item.position - agent.transform.position) < herd.SquareAvoidanceRadius) // if the distance to the item is within the avoidance radius
            {
                Vector3 dist = agent.transform.position - item.position;
                nAvoid++;
                // float forceMult = (dist.sqrMagnitude / herd.neighborRadius * herd.neighborRadius) * 100;
                // float forceMult = herd.neighborRadius - Vector3.Distance(agent.transform.position, item.position);
                //Debug.Log("player is " + Vector3.Distance(agent.transform.position, item.position) + " away.");

                avoidanceMove += dist; // add vector pointing away from item
            }
        }

        if (nAvoid > 0)
        {
            avoidanceMove /= nAvoid;             // average, avoidanceMove is now the transform of the destination
        }
        return(avoidanceMove);
    }
Пример #4
0
 public abstract Vector3 CalculateMove(OnlineHerdAgent agent, List <Transform> context, OnlineHerd herd);
Пример #5
0
 // Called once when created
 public void Initialize(OnlineHerd herd)
 {
     agentHerd = herd; // save my herd
 }
    public override Vector3 CalculateMove(OnlineHerdAgent agent, List <Transform> context, OnlineHerd herd)
    {
        Vector3 centerOffset = center - agent.transform.position;
        float   t            = centerOffset.magnitude / radius;

        if (t < 0.95)
        {
            return(Vector3.zero);         // if within 95% of the circle, don't move towards the center
        }
        return(centerOffset * t * t * t); // stronger the further from the center
    }
    public override Vector3 CalculateMove(OnlineHerdAgent agent, List <Transform> context, OnlineHerd herd)
    {
        // if no neighbors, maintain current alignment
        if (context.Count == 0)
        {
            return(Vector3.zero);
        }

        // add all neighbor's alignment together and average
        Vector3          alignmentMove = Vector3.zero;
        List <Transform> filterContext = (filter == null) ? context : filter.Filter(agent, context); // this is a filtered behavior

        foreach (Transform item in filterContext)
        {
            alignmentMove += item.GetComponent <Rigidbody>().velocity; // add the facing direction
        }
        alignmentMove /= context.Count;                                // average, alignmentMove is now the destination alignment

        return(alignmentMove);
    }
    public override Vector3 CalculateMove(OnlineHerdAgent agent, List <Transform> context, OnlineHerd herd)
    {
        // If there are no bison or players nearby, we chillin
        if (context.Count == 0)
        {
            return(Vector3.zero);
        }

        // This is what will be returned in the end
        Vector3 direction = Vector3.zero;

        //This should return the player(s), if there are any
        List <Transform> filterContext = (filter == null) ? context : filter.Filter(agent, context);

        // For each player
        foreach (Transform item in filterContext)
        {
            //Use this to see distance between bison and player(s), and increase speed depending on this
            float distance = Vector3.Distance(item.transform.position, agent.transform.position);

            //Assuming the max distance in front of a player that this behavior triggers is 6
            //multiplier output ranges from 1x to 7x
            float multiplier = 7f - distance;

            //This line makes the output instead range from 1/6 to 7/6,
            //where 1/6 is multiplier if 6 units away, and 7/6 is multiplier if 0 units away
            // (So you move slightly faster than player if right on top of them, and slower than them if further away)
            multiplier /= 6f;

            Vector3 playerDirection  = item.transform.forward;
            Vector3 bisonDirection   = agent.transform.forward;
            float   directionOutcome = Vector3.Dot(playerDirection, bisonDirection);

            //If bison is pointing in the opposite direction, double multiplier
            if (directionOutcome == -1)
            {
                multiplier *= 2;
            }

            //If bison is poining in perpendicular direction, 1.5x multiplier
            else if (directionOutcome == 0)
            {
                multiplier *= 1.5f;
            }

            direction = playerDirection;
            //direction *= multiplier;        // Comment this out if you don't want speed to change
        }

        direction *= 6; // Make sure it's visible
        return(direction);
    }
    public override Vector3 CalculateMove(OnlineHerdAgent agent, List <Transform> context, OnlineHerd herd)
    {
        // if no neighbors return no adjustment
        if (context.Count == 0)
        {
            return(Vector3.zero);
        }

        // add all points together and average
        Vector3 avoidanceMove = Vector3.zero;
        int     nAvoid        = 0;

        List <Transform> filterContext = (filter == null) ? context : filter.Filter(agent, context); // this is a filtered behavior

        // if no filtered neighbors return no adjustment
        if (filterContext.Count == 0)
        {
            return(Vector3.zero);
        }

        foreach (Transform item in filterContext)
        {
            if (Vector3.SqrMagnitude(item.position - agent.transform.position) < herd.SquareIdleAvoidanceRadius) // if the distance to the item is within the avoidance radius
            {
                nAvoid++;
                avoidanceMove += agent.transform.position - item.position; // add vector pointing away from item
            }
        }

        if (nAvoid > 0)
        {
            avoidanceMove /= nAvoid;             // average, avoidanceMove is now the transform of the destination
        }
        return(avoidanceMove);
    }
Пример #10
0
    public override Vector3 CalculateMove(OnlineHerdAgent agent, List <Transform> context, OnlineHerd herd)
    {
        // handle data mismatch
        if (behaviors.Length != weights.Length)
        {
            Debug.LogError("Data missmatch in " + name, this);
            return(Vector3.zero);
        }

        // set up move
        Vector3 move = Vector3.zero;

        // iterate through behaviors
        for (int i = 0; i < behaviors.Length; i++)
        {
            Vector3 partialMove = behaviors[i].CalculateMove(agent, context, herd) * weights[i]; // do a behavior

            if (partialMove != Vector3.zero)
            {
                if (partialMove.sqrMagnitude > weights[i] * weights[i]) // the weight caps the magnitude of each behavior
                {
                    partialMove.Normalize();
                    partialMove *= weights[i];
                }

                move += partialMove;
            }
        }

        move.y = 0; // bison don't jump on their own
        return(move);
    }
    public override Vector3 CalculateMove(OnlineHerdAgent agent, List <Transform> context, OnlineHerd herd)
    {
        // if no neighbors, maintain current alignment
        if (context.Count == 0)
        {
            return(agent.transform.forward);
        }

        // add all neighbor's alignment together and average
        Vector3          alignmentMove = Vector3.zero;
        List <Transform> filterContext = (filter == null) ? context : filter.Filter(agent, context); // this is a filtered behavior

        foreach (Transform item in filterContext)
        {
            if (Vector3.SqrMagnitude(item.position - agent.transform.position) < herd.SquareAvoidanceRadius * 1.25f) // if the distance to the item is within the avoidance radius
            {
                alignmentMove += item.transform.forward;                                                             // add the facing direction
            }
        }
        alignmentMove /= context.Count; // average, alignmentMove is now the destination alignment

        return(alignmentMove);
    }
Пример #12
0
    public override Vector3 CalculateMove(OnlineHerdAgent agent, List <Transform> context, OnlineHerd herd)
    {
        // if no neighbors return no adjustment
        if (context.Count == 0)
        {
            return(Vector3.zero);
        }

        Vector3          cohesionMove  = Vector3.zero;
        List <Transform> filterContext = (filter == null) ? context : filter.Filter(agent, context); // this is a filtered behavior

        // if no neighbors return no adjustment
        if (filterContext.Count == 0)
        {
            return(Vector3.zero);
        }
        foreach (Transform item in filterContext)  // add all points together and average
        {
            cohesionMove += item.position;
        }
        cohesionMove /= filterContext.Count; // average, cohesionMove is now the transform of the destination

        // create offset from agent position, the actual move of the agent
        if (cohesionMove == Vector3.zero)
        {
            return(cohesionMove);
        }
        cohesionMove -= agent.transform.position;

        return(cohesionMove);
    }
    public override Vector3 CalculateMove(OnlineHerdAgent agent, List <Transform> context, OnlineHerd herd)
    {
        // if no neighbors return no adjustment
        if (context.Count == 0)
        {
            return(Vector3.zero);
        }

        // add all points together and average
        Vector3          cohesionMove  = Vector3.zero;
        List <Transform> filterContext = (filter == null) ? context : filter.Filter(agent, context); // this is a filtered behavior

        foreach (Transform item in filterContext)
        {
            cohesionMove += item.position;
        }
        cohesionMove /= context.Count; // average

        // create offset from agent position, the actual move of the agent
        cohesionMove -= agent.transform.position;
        cohesionMove  = Vector3.SmoothDamp(agent.transform.forward, cohesionMove, ref currentVelocity, agentSmoothTime); // this is what makes this "smoothed"

        return(cohesionMove);
    }