Ejemplo n.º 1
0
    // Start is called before the first frame update
    void Start()
    {
        myMoveType           = new FollowPath();
        myMoveType.character = this;
        myMoveType.path      = myPath;

        myRotateType           = new LookWhereGoing();
        myRotateType.character = this;
        myRotateType.target    = myTarget;
    }
    // Start is called before the first frame update
    void Start()
    {
        myMoveType           = new Seek();
        myMoveType.character = this;
        myMoveType.target    = myTarget;

        myRotateType           = new LookWhereGoing();
        myRotateType.character = this;
        myRotateType.target    = myTarget;
    }
Ejemplo n.º 3
0
    void Start()
    {
        myMoveType             = new AvoidCollision();
        myMoveType.myCharacter = this;
        myMoveType.myTargets   = myTargets;

        myRotateType           = new LookWhereGoing();
        myRotateType.character = this;
        myRotateType.target    = myTarget;
    }
Ejemplo n.º 4
0
        // Start is called before the first frame update
        void Start()
        {
            myMoveType           = new Pursue();
            myMoveType.character = this;
            myMoveType.target    = myTarget;

            myRotateType           = new LookWhereGoing();
            myRotateType.character = this;
            myRotateType.target    = myTarget;
            character = GetComponent <ThirdPersonCharacter>();
        }
Ejemplo n.º 5
0
    // Start is called before the first frame update
    void Start()
    {
        myMoveType           = new ObstacleAvoidance();
        myMoveType.character = this;
        myMoveType.target    = myTarget;


        myRotateType           = new LookWhereGoing();
        myRotateType.character = this;
        myRotateType.target    = myTarget;
    }
Ejemplo n.º 6
0
    // Start is called before the first frame update
    void Start()
    {
        nodes = GameObject.FindGameObjectsWithTag("Node");

        myRotateType           = new LookWhereGoing();
        myRotateType.character = this;
        myRotateType.target    = myTarget;

        myMoveType           = new PathFollow();
        myMoveType.character = this;
        getPath();
    }
    // Start is called before the first frame update
    void Start()
    {
        batter = FindObjectOfType <PathFollower>();

        myMoveType           = new Arrive();
        myMoveType.character = this;
        myMoveType.target    = myTarget;

        myRotateType           = new LookWhereGoing();
        myRotateType.character = this;
        myRotateType.target    = myTarget;
    }
Ejemplo n.º 8
0
    private void Awake()
    {
        steering = new BlendedSteering();

        //Create behaviors to go into the bird
        Arrive arriveAI = new Arrive();

        arriveAI.character = GetComponent <Kinematic>();
        arriveAI.target    = arriveTarget;

        Seperation seperateAI = new Seperation();

        seperateAI.character = GetComponent <Kinematic>();
        GameObject[] allBirds = GameObject.FindGameObjectsWithTag("bird");
        Kinematic[]  allOtherBirdKinematics = new Kinematic[allBirds.Length];
        int          j = 0;

        foreach (GameObject bird in allBirds)
        {
            if (bird != this)
            {
                allOtherBirdKinematics[j] = bird.GetComponent <Kinematic>();
                j++;
            }
            else
            {
                Debug.Log("caught myself");
            }
        }
        seperateAI.targets = allOtherBirdKinematics;

        LookWhereGoing lookAI = new LookWhereGoing();

        lookAI.character = GetComponent <Kinematic>();

        //Set up the blended steering array
        //We need three for Seperation, Arrive, and LookWhereGoing
        steering.behaviors    = new BehaviorAndWeight[3];
        steering.behaviors[0] = new BehaviorAndWeight();
        steering.behaviors[1] = new BehaviorAndWeight();
        steering.behaviors[2] = new BehaviorAndWeight();

        //Assign the specific behaviors
        steering.behaviors[0].behavior = arriveAI;
        steering.behaviors[1].behavior = seperateAI;
        steering.behaviors[2].behavior = lookAI;

        //Now set the weight of each behavior
        steering.behaviors[0].weight = 1f;
        steering.behaviors[1].weight = 20f;
        steering.behaviors[2].weight = 4f;
    }
Ejemplo n.º 9
0
    void Start()
    {
        // Separate from other birds
        Separation separate = new Separation();

        separate.character = this;
        GameObject[] goBirds = GameObject.FindGameObjectsWithTag("bird");
        kBirds = new GameObject[goBirds.Length - 1];
        int j = 0;

        for (int i = 0; i < goBirds.Length - 1; i++)
        {
            if (goBirds[i] == this)
            {
                continue;
            }
            kBirds[j++] = goBirds[i];
        }
        separate.targets = kBirds;

        // Cohere to center of mass - ez mode
        Seek cohere = new Seek();

        cohere.character = this;
        cohere.target    = myCohereTarget;

        Pursue pursue = new Pursue();

        pursue.character = this;
        pursue.target    = myTarget;

        // look where center of mass is going - ez mode
        LookWhereGoing myRotateType = new LookWhereGoing();

        myRotateType.character = this;

        mySteering                       = new BlendedSteering();
        mySteering.behaviors             = new BehaviorAndWeight[4];
        mySteering.behaviors[0]          = new BehaviorAndWeight();
        mySteering.behaviors[0].behavior = separate;
        mySteering.behaviors[0].weight   = .2f;
        mySteering.behaviors[1]          = new BehaviorAndWeight();
        mySteering.behaviors[1].behavior = cohere;
        mySteering.behaviors[1].weight   = .3f;
        mySteering.behaviors[2]          = new BehaviorAndWeight();
        mySteering.behaviors[2].behavior = pursue;
        mySteering.behaviors[2].weight   = .5f;
        mySteering.behaviors[3]          = new BehaviorAndWeight();
        mySteering.behaviors[3].behavior = myRotateType;
        mySteering.behaviors[3].weight   = 1f;
    }
Ejemplo n.º 10
0
    //public bool needArrive = false;

    // Start is called before the first frame update
    void Start()
    {
        myMoveType           = new FollowPath();
        myMoveType.character = this;
        myMoveType.wayPts    = myWayPts;

        myArriveType           = new Arrive();
        myArriveType.character = this;
        myArriveType.target    = myWayPts[myMoveType.wayPts.Length - 1].gameObject;

        myRotateType           = new LookWhereGoing();
        myRotateType.character = this;
        myRotateType.target    = myTarget;
    }
    // Start is called before the first frame update
    protected virtual void Start()
    {
        arrive      = new Arrive();
        look        = new LookWhereGoing();
        seperate    = new Seperation();
        avoid       = new ObstacleAvoidance();
        flockGroups = new BlendedSteering[3];

        arrive.target = target;
        arrive.ai     = this;
        look.target   = target;
        look.ai       = this;
        seperate.ai   = this;
        avoid.ai      = this;

        AbstractKinematic[] kBirds;
        GameObject[]        birds = GameObject.FindGameObjectsWithTag("Bird");
        kBirds = new AbstractKinematic[birds.Length - 1];
        int j = 0;

        for (int i = 0; i < birds.Length - 1; i++)
        {
            if (birds[i] == this)
            {
                continue;
            }
            kBirds[j++] = birds[i].GetComponent <AbstractKinematic>();
        }
        seperate.targets = kBirds;

        InitializePriorityOne();
        InitializePriorityTwo();
        InitializePriorityThree();

        /*
         * flocking.behaviors = new BehaviorAndWeight[3];
         * BehaviorAndWeight behavior1 = new BehaviorAndWeight();
         * behavior1.behavior = arrive;
         * behavior1.weight = 100f;
         * flocking.behaviors[0] = behavior1;
         * BehaviorAndWeight behavior2 = new BehaviorAndWeight();
         * behavior2.behavior = look;
         * behavior2.weight = 1f;
         * flocking.behaviors[1] = behavior2;
         * BehaviorAndWeight behavior3 = new BehaviorAndWeight();
         * behavior3.behavior = seperate;
         * behavior3.weight = 20f;
         * flocking.behaviors[2] = behavior3;
         */
    }
Ejemplo n.º 12
0
    // Update is called once per frame
    void Update()
    {
        //update position and rotation
        transform.position    += linearVelocity * Time.deltaTime;
        transform.eulerAngles += new Vector3(0, angularVelocity * Time.deltaTime, 0);

        //update linear and angular velocities
        switch (mode)
        {
        case 0:
            Align myAlign = new Align();
            myAlign.character = this;
            myAlign.target    = target;
            steering          = myAlign.getSteering();
            if (steering != null)
            {
                linearVelocity  += steering.linear * Time.deltaTime;
                angularVelocity += steering.angular * Time.deltaTime;
            }
            Debug.Log("Align");
            break;

        case 1:
            Face myFace = new Face();
            myFace.character = this;
            myFace.target    = target;
            steering         = myFace.getSteering();
            if (steering != null)
            {
                linearVelocity  += steering.linear * Time.deltaTime;
                angularVelocity += steering.angular * Time.deltaTime;
            }
            Debug.Log("Face");
            break;

        case 2:
            LookWhereGoing myLook = new LookWhereGoing();
            myLook.character = this;
            myLook.target    = target;
            steering         = myLook.getSteering();
            if (steering != null)
            {
                linearVelocity  += steering.linear * Time.deltaTime;
                angularVelocity += steering.angular * Time.deltaTime;
            }
            Debug.Log("Look");
            break;
        }
    }
Ejemplo n.º 13
0
    // Start is called before the first frame update
    void Start()
    {
        myMoveType           = new AvoidObstacle();
        myMoveType.character = this;
        myMoveType.target    = myTarget;
        myMoveType.flee      = flee;

        mySeekRotateType           = new Face();
        mySeekRotateType.character = this;
        mySeekRotateType.target    = myTarget;

        myFleeRotateType           = new LookWhereGoing();
        myFleeRotateType.character = this;
        myFleeRotateType.target    = myTarget;
    }
Ejemplo n.º 14
0
    void Start()
    {
        myRotateType           = new LookWhereGoing();
        myRotateType.character = this;
        myRotateType.target    = myTarget;

        if (gameObject.name == "Pathfinder01")
        {
            optimalPath();
        }
        if (gameObject.name == "Pathfinder02")
        {
            weightedPath();
        }
    }
    // Start is called before the first frame update
    void Start()
    {
        myMoveType           = new Pursue();
        myMoveType.character = this;
        myMoveType.target    = myTarget;
        myMoveType.evade     = evade;

        myPursueRotateType           = new Face();
        myPursueRotateType.character = this;
        myPursueRotateType.target    = myTarget;

        myEvadeRotateType           = new LookWhereGoing();
        myEvadeRotateType.character = this;
        myEvadeRotateType.target    = myTarget;
    }
    // Start is called before the first frame update
    void Start()
    {
        myRotateType           = new LookWhereGoing();
        myRotateType.character = this;
        myRotateType.target    = myTarget;

        Graph myGraph = new Graph();

        myGraph.nodes = graphNode;
        myGraph.buildGraph();
        path = d.pathfindDijkstra(myGraph, start, goal);
        path.Add(new Graph.Connection(goal, start));
        myMoveType           = new FollowPath();
        myMoveType.targets   = path;
        myMoveType.character = this;
    }
Ejemplo n.º 17
0
    // Start is called before the first frame update
    protected virtual void Start()
    {
        mySeek           = new Seek();
        mySeek.character = this;
        mySeek.target    = target;
        myFlee           = new Flee();
        myFlee.character = this;
        myFlee.target    = target;

        myArrv           = new Arrive();
        myArrv.character = this;
        myArrv.target    = target;

        myAlgn           = new Align();
        myAlgn.character = this;
        myAlgn.target    = target;

        myFace           = new Face();
        myFace.character = this;
        myFace.target    = target;

        myLWYG           = new LookWhereGoing();
        myLWYG.character = this;
        myLWYG.target    = target;

        myFPth           = new FollowPathDemo();
        myFPth.character = this;
        myFPth.path      = waypointList;

        mySepr           = new Separation();
        mySepr.character = this;

        myPrsu           = new Pursue();
        myPrsu.character = this;
        myPrsu.target    = target;

        myClAv           = new CollAvoid();
        myClAv.character = this;
        myClAv.targets   = collAvoidTargets;

        myObAv           = new ObstAvoid();
        myObAv.character = this;

        controlledSteeringUpdate = new SteeringOutput();
    }
Ejemplo n.º 18
0
    void StandardBehavior()
    {
        UpdatePositionRotation();
        if (myTarget != null)
        {
            Arrive arrive = new Arrive();
            arrive.character = this;
            arrive.target    = myTarget;
            SteeringOutput arriveSteering = arrive.getSteering();
            if (arriveSteering != null)
            {
                linearVelocity += arriveSteering.linear * Time.deltaTime;
                //angularVelocity += arriveSteering.angular * Time.deltaTime;
            }
        }

        if (lookWhereGoing == true)
        {
            LookWhereGoing face = new LookWhereGoing();
            face.character = this;
            face.target    = faceTarget;
            SteeringOutput lookSteering = face.getSteering();
            if (lookSteering != null)
            {
                //linearVelocity += lookSteering.linear * Time.deltaTime;
                angularVelocity += lookSteering.angular * Time.deltaTime;
            }
            return;
        }

        if (faceTarget != null)
        {
            Face face = new Face();
            face.character = this;
            face.target    = faceTarget;
            SteeringOutput lookSteering = face.getSteering();
            if (lookSteering != null)
            {
                //linearVelocity += lookSteering.linear * Time.deltaTime;
                angularVelocity += lookSteering.angular * Time.deltaTime;
            }
        }
    }
Ejemplo n.º 19
0
    void RotAI(int mode)
    {
        switch (mode)
        {
        case 0:
            Align myAlign = new Align();
            myAlign.character = this;
            myAlign.target    = target;
            steering          = myAlign.getSteering();
            if (steering != null)
            {
                angularVelocity += steering.angular * Time.deltaTime;
            }
            Debug.Log("Align");
            break;

        case 1:
            Face myFace = new Face();
            myFace.character = this;
            myFace.target    = target;
            steering         = myFace.getSteering();
            if (steering != null)
            {
                angularVelocity += steering.angular * Time.deltaTime;
            }
            Debug.Log("Face");
            break;

        case 2:
            LookWhereGoing myLook = new LookWhereGoing();
            myLook.character = this;
            myLook.target    = target;
            steering         = myLook.getSteering();
            if (steering != null)
            {
                angularVelocity += steering.angular * Time.deltaTime;
            }
            Debug.Log("Look");
            break;
        }
    }
Ejemplo n.º 20
0
    // Start is called before the first frame update
    void Start()
    {
        if (start == null)
        {
            start = GameObject.Find("node__Instance1").GetComponent <Node>();
        }
        if (goal == null)
        {
            goal = GameObject.Find("node__Instance100").GetComponent <Node>();
        }

        myRotateType           = new LookWhereGoing();
        myRotateType.character = this;
        myRotateType.target    = target;

        Graph myGraph = new Graph();

        myGraph.Build();
        List <Connection> path = Dijkstra.pathfind(myGraph, start, goal);

        // path is a list of connections - convert this to gameobjects for the FollowPath steering behavior

        myPath = new GameObject[path.Count + 1];
        int i = 0;

        foreach (Connection c in path)
        {
            Debug.Log("from " + c.getFromNode() + " to " + c.getToNode() + " @" + c.getCost());
            myPath[i] = c.getFromNode().gameObject;
            i++;
        }
        myPath[i] = goal.gameObject;

        myMoveType           = new FollowPathDemo();
        myMoveType.character = this;
        myMoveType.path      = myPath;
    }
Ejemplo n.º 21
0
    // Start is called before the first frame update
    void Start()
    {
        mySeek           = new Seek();
        mySeek.character = this;
        mySeek.target    = target;
        myFlee           = new Flee();
        myFlee.character = this;
        myFlee.target    = target;

        myArrv           = new Arrive();
        myArrv.character = this;
        myArrv.target    = target;

        myAlgn           = new Align();
        myAlgn.character = this;
        myAlgn.target    = target;

        myFace           = new Face();
        myFace.character = this;
        myFace.target    = target;

        myLWYG           = new LookWhereGoing();
        myLWYG.character = this;
        myLWYG.target    = target;

        myFPth           = new FollowPathDemo();
        myFPth.character = this;
        myFPth.path      = waypointList;

        mySepr           = new Separation();
        mySepr.character = this;

        myPrsu           = new Pursue();
        myPrsu.character = this;
        myPrsu.target    = target;
    }
    void Update()
    {
        SteeringOutput myMovement = path.GetSteering();
        LookWhereGoing look       = new LookWhereGoing();

        look.target = path.target;
        look.ai     = this;
        SteeringOutput mySteering = look.GetSteering();

        linearVelocity     += myMovement.linear * Time.deltaTime;
        transform.position += linearVelocity * Time.deltaTime;
        if (linearVelocity.magnitude > maxSpeed)
        {
            linearVelocity.Normalize();
            linearVelocity *= maxSpeed;
        }

        /*
         * angularVelocity += mySteering.angular * Time.deltaTime;
         * if (float.IsNaN(angularVelocity))
         *  angularVelocity = 0;
         * transform.eulerAngles += new Vector3(0, angularVelocity * Time.deltaTime, 0);
         */
    }
Ejemplo n.º 23
0
    void Start()
    {
        // Separate from other birds
        Separation separate = new Separation();

        separate.character = this;
        GameObject[] goBirds = GameObject.FindGameObjectsWithTag("bird");
        kBirds = new Kinematic[goBirds.Length - 1];
        int j = 0;

        for (int i = 0; i < goBirds.Length - 1; i++)
        {
            if (goBirds[i] == this)
            {
                continue;
            }
            kBirds[j++] = goBirds[i].GetComponent <Kinematic>();
        }
        separate.targets = kBirds;

        // Cohere to center of mass - ez mode
        Arrive cohere = new Arrive();

        cohere.character = this;
        cohere.target    = myCohereTarget;

        // look where center of mass is going - ez mode
        LookWhereGoing myRotateType = new LookWhereGoing();

        myRotateType.character = this;

        mySteering                       = new BlendedSteering();
        mySteering.behaviors             = new BehaviorAndWeight[3];
        mySteering.behaviors[0]          = new BehaviorAndWeight();
        mySteering.behaviors[0].behavior = separate;
        mySteering.behaviors[0].weight   = 1f; //3
        mySteering.behaviors[1]          = new BehaviorAndWeight();
        mySteering.behaviors[1].behavior = cohere;
        mySteering.behaviors[1].weight   = 1f; //.5
        mySteering.behaviors[2]          = new BehaviorAndWeight();
        mySteering.behaviors[2].behavior = myRotateType;
        mySteering.behaviors[2].weight   = 1f;

        // set up prioritysteering
        ObstacleAvoidance myAvoid = new ObstacleAvoidance();

        myAvoid.character = this;
        myAvoid.target    = myCohereTarget;
        myAvoid.flee      = true; // otherwise I seek to the obstacle
        BlendedSteering myHighPrioritySteering = new BlendedSteering();

        myHighPrioritySteering.behaviors             = new BehaviorAndWeight[1];
        myHighPrioritySteering.behaviors[0]          = new BehaviorAndWeight();
        myHighPrioritySteering.behaviors[0].behavior = myAvoid;
        myHighPrioritySteering.behaviors[0].weight   = 1f;

        myAdvancedSteering           = new PrioritySteering();
        myAdvancedSteering.groups    = new BlendedSteering[2];
        myAdvancedSteering.groups[0] = new BlendedSteering();
        myAdvancedSteering.groups[0] = myHighPrioritySteering;
        myAdvancedSteering.groups[1] = new BlendedSteering();
        myAdvancedSteering.groups[1] = mySteering;
    }
Ejemplo n.º 24
0
    void MainSteeringBehaviors()
    {
        ResetOrientation();

        switch (choiceOfBehavior)
        {
        case SteeringBehaviors.Seek:
            Seek seek = new Seek();
            seek.character = this;
            seek.target    = newTarget;
            SteeringOutput seeking = seek.getSteering();
            if (seeking != null)
            {
                linear  += seeking.linear * Time.deltaTime;
                angular += seeking.angular * Time.deltaTime;
            }
            break;

        case SteeringBehaviors.Flee:
            Flee flee = new Flee();
            flee.character = this;
            flee.target    = newTarget;
            SteeringOutput fleeing = flee.getSteering();
            if (fleeing != null)
            {
                linear  += fleeing.linear * Time.deltaTime;
                angular += fleeing.angular * Time.deltaTime;
            }
            break;

        case SteeringBehaviors.Align:
            Align align = new Align();
            align.character = this;
            align.target    = newTarget;
            SteeringOutput aligning = align.getSteering();
            if (aligning != null)
            {
                linear  += aligning.linear * Time.deltaTime;
                angular += aligning.angular * Time.deltaTime;
            }
            break;

        case SteeringBehaviors.Face:
            Face face = new Face();
            face.character = this;
            face.target    = newTarget;
            SteeringOutput facing = face.getSteering();
            if (facing != null)
            {
                linear  += facing.linear * Time.deltaTime;
                angular += facing.angular * Time.deltaTime;
            }
            break;

        case SteeringBehaviors.LookWhereGoing:
            LookWhereGoing look = new LookWhereGoing();
            look.character = this;
            look.target    = newTarget;
            SteeringOutput looking = look.getSteering();
            if (looking != null)
            {
                linear  += looking.linear * Time.deltaTime;
                angular += looking.angular * Time.deltaTime;
            }
            break;

        case SteeringBehaviors.Arrive:
            Arrive arrive = new Arrive();
            arrive.character = this;
            arrive.target    = newTarget;
            SteeringOutput arriving = arrive.getSteering();
            if (arriving != null)
            {
                linear  += arriving.linear * Time.deltaTime;
                angular += arriving.angular * Time.deltaTime;
            }
            break;

        case SteeringBehaviors.PathFollow:

            follow.character = this;
            lookwg.character = this;

            follow.path   = myPath;
            lookwg.target = newTarget;

            SteeringOutput following = follow.getSteering();
            SteeringOutput lookingwg = lookwg.getSteering();

            if (following != null)
            {
                linear += following.linear * Time.deltaTime;
                // angular += lookingwg.angular* Time.deltaTime;
            }
            break;

        case SteeringBehaviors.Pursue:
            Pursue         pursue = new Pursue();
            LookWhereGoing PursuelookWhereGoing = new LookWhereGoing();
            pursue.character = this;
            PursuelookWhereGoing.character = this;
            pursue.target = newTarget;
            PursuelookWhereGoing.target = newTarget;
            SteeringOutput pursuing = pursue.getSteering();
            SteeringOutput pursuelookingWhereGoing = PursuelookWhereGoing.getSteering();

            if (pursuing != null)
            {
                if (pursuing.linear.magnitude <= maxSpeed)
                {
                    linear += pursuing.linear * Time.deltaTime;
                }
                angular += pursuing.angular * Time.deltaTime;
            }
            break;

        case SteeringBehaviors.Separate:
            Separation     separate       = new Separation();
            LookWhereGoing lookWhereGoing = new LookWhereGoing();
            separate.character       = this;
            lookWhereGoing.character = this;
            separate.targets         = targets;
            lookWhereGoing.target    = newTarget;
            SteeringOutput lookingWhereGoing = lookWhereGoing.getSteering();
            SteeringOutput separating        = separate.getSteering();
            if (separating != null)
            {
                linear  += separating.linear * Time.deltaTime;
                angular += separating.angular * Time.deltaTime;
            }
            break;

        case SteeringBehaviors.CollisionAvoidance:
            CollisionAvoidance avoid = new CollisionAvoidance();
            LookWhereGoing     AvoidlookWhereGoing = new LookWhereGoing();
            avoid.character = this;
            AvoidlookWhereGoing.character = this;
            avoid.targets = targets;
            AvoidlookWhereGoing.target = newTarget;
            SteeringOutput AvoidlookingWhereGoing = AvoidlookWhereGoing.getSteering();
            SteeringOutput avoiding = avoid.getSteering();
            if (avoiding != null)
            {
                linear  += avoiding.linear * Time.deltaTime;
                angular += avoiding.angular * Time.deltaTime;
            }
            break;

        case SteeringBehaviors.ObstacleAvoidance:
            ObstacleAvoidance obAvoid = new ObstacleAvoidance();
            LookWhereGoing    obAvoidlookWhereGoing = new LookWhereGoing();
            obAvoid.character = this;
            obAvoidlookWhereGoing.character = this;
            obAvoid.target = newTarget;
            obAvoidlookWhereGoing.target = newTarget;
            SteeringOutput obAvoiding = obAvoid.getSteering();
            SteeringOutput obAvoidlookingWhereGoing = obAvoidlookWhereGoing.getSteering();
            if (obAvoiding != null)
            {
                if (obAvoiding.linear.magnitude <= maxSpeed)
                {
                    linear += obAvoiding.linear * Time.deltaTime;
                }
                angular += obAvoiding.angular * Time.deltaTime;
            }
            break;

        case SteeringBehaviors.Flocking:
            Separation      sepFlock    = new Separation();
            Arrive          arriveFlock = new Arrive();
            LookWhereGoing  lwgFlock    = new LookWhereGoing();
            BlendedSteering mySteering  = new BlendedSteering();
            Kinematic[]     kBirds;
            sepFlock.character = this;
            GameObject[] goBirds = GameObject.FindGameObjectsWithTag("Pengu");
            kBirds = new Kinematic[goBirds.Length - 1];
            int j = 0;
            for (int i = 0; i < goBirds.Length - 1; i++)
            {
                if (goBirds[i] == this)
                {
                    continue;
                }
                goBirds[i].GetComponent <Animator>().SetInteger("Walk", 1);
                kBirds[j++] = goBirds[i].GetComponent <Kinematic>();
            }
            sepFlock.targets = kBirds;

            arriveFlock.character = this;
            //Debug.Log(arriveFlock.character);
            arriveFlock.target = newTarget;
            //Debug.Log(arriveFlock.target);
            lwgFlock.character               = this;
            lwgFlock.target                  = newTarget;
            mySteering.behaviors             = new BehaviorAndWeight[3];
            mySteering.behaviors[0]          = new BehaviorAndWeight();
            mySteering.behaviors[0].behavior = sepFlock;
            mySteering.behaviors[0].weight   = 1f;
            mySteering.behaviors[1]          = new BehaviorAndWeight();
            mySteering.behaviors[1].behavior = arriveFlock;
            mySteering.behaviors[1].weight   = 1f;
            mySteering.behaviors[2]          = new BehaviorAndWeight();
            mySteering.behaviors[2].behavior = lwgFlock;
            mySteering.behaviors[2].weight   = 1f;

            ObstacleAvoidance myAvoid = new ObstacleAvoidance();
            myAvoid.character = this;
            myAvoid.target    = newTarget;
            myAvoid.flee      = true;

            BlendedSteering myHighPrioritySteering = new BlendedSteering();
            myHighPrioritySteering.behaviors             = new BehaviorAndWeight[1];
            myHighPrioritySteering.behaviors[0]          = new BehaviorAndWeight();
            myHighPrioritySteering.behaviors[0].behavior = myAvoid;
            myHighPrioritySteering.behaviors[0].weight   = 0.1f;
            myAdvancedSteering.groups    = new BlendedSteering[2];
            myAdvancedSteering.groups[0] = new BlendedSteering();
            myAdvancedSteering.groups[0] = myHighPrioritySteering;
            myAdvancedSteering.groups[1] = new BlendedSteering();
            myAdvancedSteering.groups[1] = mySteering;

            //steeringUpdate = mySteering.getSteering();
            avoidObstacles = true;
            if (!avoidObstacles)
            {
                steeringUpdate = mySteering.getSteering();
                if (steeringUpdate != null)
                {
                    linear  += steeringUpdate.linear * Time.deltaTime;
                    angular += steeringUpdate.angular * Time.deltaTime;
                }
            }
            else
            {
                steeringUpdate = myAdvancedSteering.getSteering();
                if (steeringUpdate != null)
                {
                    linear  += steeringUpdate.linear * Time.deltaTime;
                    angular += steeringUpdate.angular * Time.deltaTime;
                }
            }
            break;

        case SteeringBehaviors.PathFinder:
            SteeringOutput lwyger = myRotateType.getSteering();
            linear  += myMoveType.getSteering().linear *Time.deltaTime;
            angular += myMoveType.getSteering().angular *Time.deltaTime;
            break;
        }
    }
Ejemplo n.º 25
0
    void MainSteeringBehaviors()
    {
        ResetOrientation();

        switch (choiceOfBehavior)
        {
        case steeringBehaviors.Seek:
            Seek seek = new Seek();
            seek.character = this;
            seek.target    = newTarget;
            SteeringOutput seeking = seek.getSteering();
            if (seeking != null)
            {
                linearVelocity  += seeking.linearVelocity * Time.deltaTime;
                angularVelocity += seeking.angularVelocity * Time.deltaTime;
            }
            break;

        case steeringBehaviors.Flee:
            Flee flee = new Flee();
            flee.character = this;
            flee.target    = newTarget;
            SteeringOutput fleeing = flee.getSteering();
            if (fleeing != null)
            {
                linearVelocity  += fleeing.linearVelocity * Time.deltaTime;
                angularVelocity += fleeing.angularVelocity * Time.deltaTime;
            }
            break;

        case steeringBehaviors.Align:
            Align align = new Align();
            align.character = this;
            align.target    = newTarget;
            SteeringOutput aligning = align.getSteering();
            if (aligning != null)
            {
                linearVelocity  += aligning.linearVelocity * Time.deltaTime;
                angularVelocity += aligning.angularVelocity * Time.deltaTime;
            }
            break;

        case steeringBehaviors.Face:
            Face face = new Face();
            face.character = this;
            face.target    = newTarget;
            SteeringOutput facing = face.getSteering();
            if (facing != null)
            {
                linearVelocity  += facing.linearVelocity * Time.deltaTime;
                angularVelocity += facing.angularVelocity * Time.deltaTime;
            }
            break;

        case steeringBehaviors.LookWhereGoing:
            LookWhereGoing look = new LookWhereGoing();
            look.character = this;
            look.target    = newTarget;
            SteeringOutput looking = look.getSteering();
            if (looking != null)
            {
                linearVelocity  += looking.linearVelocity * Time.deltaTime;
                angularVelocity += looking.angularVelocity * Time.deltaTime;
            }
            break;

        case steeringBehaviors.Arrive:
            Arrive arrive = new Arrive();
            arrive.character = this;
            arrive.target    = newTarget;
            SteeringOutput arriving = arrive.getSteering();
            if (arriving != null)
            {
                linearVelocity  += arriving.linearVelocity * Time.deltaTime;
                angularVelocity += arriving.angularVelocity * Time.deltaTime;
            }
            break;
        }
    }
    // Update is called once per frame
    void Update()
    {
        //Debug.Log(target);
        // Check distance to see if steering behavior should be applied to AI
        // float targetDist = (transform.position - target.transform.position).magnitude;
        // if (!DistanceActivation || targetDist >= activationRange) { }
        transform.position += linearVelocity * Time.deltaTime;
        // adding angular velocity to current transform rotation y component
        if (float.IsNaN(angularVelocity))
        {
            angularVelocity = 0;
        }
        transform.eulerAngles += new Vector3(0, angularVelocity * Time.deltaTime, 0);
        // control to switch to proper steering behavior
        if (avoidObstacles)
        {
            ObstacleAvoidance avoid = new ObstacleAvoidance();
            avoid.ai = this;
            SteeringOutput avoidForce = avoid.GetSteering();
            if (avoidForce != null)
            {
                linearVelocity += avoidForce.linear;
            }
        }
        if (seperationObstacles.Length > 0)
        {
            Seperation seperation = new Seperation();
            seperation.targets = seperationObstacles;
            seperation.ai      = this;
            SteeringOutput seperateForce = seperation.GetSteering();
            // check to see if steering is greater than zero and lock out control from other steering
            if (seperateForce.linear.magnitude > 0)
            {
                seperating = true;
            }
            else
            {
                seperating = false;
            }
            linearVelocity += seperateForce.linear * Time.deltaTime;
        }
        if (collisionAvoidance.Length > 0)
        {
            CollisionAvoidance avoidKinematic = new CollisionAvoidance();
            avoidKinematic.ai      = this;
            avoidKinematic.targets = collisionAvoidance;
            SteeringOutput avoidKinematicForce = avoidKinematic.GetSteering();
            if (avoidKinematicForce != null)
            {
                linearVelocity += avoidKinematicForce.linear;
            }
        }

        switch (movementType)
        {
        case "seek":
            Seek mySeek = new Seek();
            mySeek.ai = this;
            // if seek is false set seek property on class to false to activate flee
            mySeek.seek   = true;
            mySeek.target = target;
            SteeringOutput steeringSeek = mySeek.GetSteering();
            if (!seperating)
            {
                linearVelocity += steeringSeek.linear * Time.deltaTime;
            }
            if (linearVelocity.magnitude > maxSpeed)
            {
                linearVelocity.Normalize();
                linearVelocity *= maxSpeed;
            }
            break;

        case "flee":
            Seek myFlee = new Seek();
            myFlee.ai = this;
            // if seek is false set seek property on class to false to activate flee
            myFlee.seek   = false;
            myFlee.target = target;
            SteeringOutput steeringFlee = myFlee.GetSteering();
            if (!seperating)
            {
                linearVelocity += steeringFlee.linear * Time.deltaTime;
            }
            if (linearVelocity.magnitude > maxSpeed)
            {
                linearVelocity.Normalize();
                linearVelocity *= maxSpeed;
            }
            break;

        case "arrive":
            Arrive myArrive = new Arrive();
            myArrive.ai     = this;
            myArrive.target = target;
            SteeringOutput steeringArrive = myArrive.GetSteering();
            if (!seperating)
            {
                linearVelocity += steeringArrive.linear * Time.deltaTime;
            }
            break;

        case "pursue":
            Pursue myPursue = new Pursue();
            myPursue.ai     = this;
            myPursue.target = target;
            SteeringOutput steeringPursue = myPursue.GetSteering();
            if (!seperating)
            {
                linearVelocity += steeringPursue.linear * Time.deltaTime;
            }
            if (linearVelocity.magnitude > maxSpeed)
            {
                linearVelocity.Normalize();
                linearVelocity *= maxSpeed;
            }
            break;

        case "evade":
            Pursue myEvade = new Pursue();
            myEvade.ai     = this;
            myEvade.target = target;
            // This changes the seek flag in the parent Seek class of Pursue, sending it the flee vector instead
            myEvade.seek = false;
            SteeringOutput steeringEvade = myEvade.GetSteering();
            if (!seperating)
            {
                linearVelocity += steeringEvade.linear * Time.deltaTime;
            }
            if (linearVelocity.magnitude > maxSpeed)
            {
                linearVelocity.Normalize();
                linearVelocity *= maxSpeed;
            }
            break;

        default:
            // provide no input
            break;
            // If obstacles have been provided, return steering to seperate from them
        }

        switch (rotationType)
        {
        case "face":
            Face myFace = new Face();
            myFace.ai     = this;
            myFace.target = target;
            SteeringOutput steeringFace = myFace.GetSteering();
            if (steeringFace != null)
            {
                //    linearVelocity += steering.linear * Time.deltaTime;
                angularVelocity += steeringFace.angular * Time.deltaTime;
            }

            break;

        case "align":
            Align myAlign = new Align();
            myAlign.ai     = this;
            myAlign.target = target;
            SteeringOutput steeringAlign = myAlign.GetSteering();
            if (steeringAlign != null)
            {
                //linearVelocity += steering.linear * Time.deltaTime;
                angularVelocity += steeringAlign.angular * Time.deltaTime;
            }
            break;

        case "look":
            LookWhereGoing myLook = new LookWhereGoing();
            myLook.ai     = this;
            myLook.target = target;
            SteeringOutput steeringLook = myLook.GetSteering();
            if (steeringLook != null)
            {
                //linearVelocity += steering.linear * Time.deltaTime;
                angularVelocity += steeringLook.angular * Time.deltaTime;
            }
            break;

        default:
            //provide no input
            break;
        }
    }
    // Update is called once per frame
    void Update()
    {
        transform.position += linearVelocity * Time.deltaTime;
        // adding angular velocity to current transform rotation y component
        if (float.IsNaN(angularVelocity))
        {
            angularVelocity = 0;
        }
        transform.eulerAngles += new Vector3(0, angularVelocity * Time.deltaTime, 0);
        //dynamicSteering steering = new Seek();
        // control to switch to proper steering behavior
        if (!arrive)
        {
            Seek mySeek = new Seek();
            mySeek.ai = this;
            // if seek is false set seek property on class to false to activate flee
            if (!seek)
            {
                mySeek.seek = false;
            }
            else
            {
                mySeek.seek = true;
            }
            mySeek.target = target;
            SteeringOutput steering = mySeek.GetSteering();
            linearVelocity  += steering.linear * Time.deltaTime;
            angularVelocity += steering.angular * Time.deltaTime;
            if (linearVelocity.magnitude > maxSpeed)
            {
                linearVelocity.Normalize();
                linearVelocity *= maxSpeed;
            }
        }
        else
        {
            Arrive myArrive = new Arrive();
            myArrive.ai     = this;
            myArrive.target = target;
            SteeringOutput steering = myArrive.GetSteering();

            linearVelocity  += steering.linear * Time.deltaTime;
            angularVelocity += steering.angular * Time.deltaTime;
        }
        if (align)
        {
            Align myAlign = new Align();
            myAlign.ai     = this;
            myAlign.target = target;
            SteeringOutput steering = myAlign.GetSteering();
            if (steering != null)
            {
                linearVelocity  += steering.linear * Time.deltaTime;
                angularVelocity += steering.angular * Time.deltaTime;
            }
        }
        if (lookWhereGoing && !align && !face)
        {
            LookWhereGoing myLook = new LookWhereGoing();
            myLook.ai     = this;
            myLook.target = target;
            SteeringOutput steering = myLook.GetSteering();
            if (steering != null)
            {
                linearVelocity  += steering.linear * Time.deltaTime;
                angularVelocity += steering.angular * Time.deltaTime;
            }
            else
            {
                Debug.Log("Returning Null");
            }
        }
        if (!lookWhereGoing && !align && face)
        {
            Face myFace = new Face();
            myFace.ai     = this;
            myFace.target = target;
            SteeringOutput steering = myFace.GetSteering();
            if (steering != null)
            {
                linearVelocity  += steering.linear * Time.deltaTime;
                angularVelocity += steering.angular * Time.deltaTime;
            }
        }
    }
    private void Start()
    {
        //Separate
        Separation separation = new Separation();

        separation.character = this;
        GameObject[] goBirds = GameObject.FindGameObjectsWithTag("Bee");

        kBirds = new Kinematic[goBirds.Length - 1];

        int a = 0;

        for (int i = 0; i < goBirds.Length - 1; i++)
        {
            if (goBirds[i] == this)
            {
                continue;
            }
            kBirds[a++] = goBirds[i].GetComponent <Kinematic>();
        }
        separation.targets = kBirds;

        //Cohere
        Arrive cohere = new Arrive();

        cohere.character = this;
        cohere.target    = myCohereTarget;

        //Rotate
        LookWhereGoing myRotateType = new LookWhereGoing();

        myRotateType.character = this;

        blendedSteering                       = new BlendedSteering();
        blendedSteering.behaviors             = new WeightedBehavior[3];
        blendedSteering.behaviors[0]          = new WeightedBehavior();
        blendedSteering.behaviors[0].behavior = separation;
        blendedSteering.behaviors[0].weight   = 1f;
        blendedSteering.behaviors[1]          = new WeightedBehavior();
        blendedSteering.behaviors[1].behavior = cohere;
        blendedSteering.behaviors[1].weight   = 1f;
        blendedSteering.behaviors[2]          = new WeightedBehavior();
        blendedSteering.behaviors[2].behavior = myRotateType;
        blendedSteering.behaviors[2].weight   = 1f;

        //Priority Arbitration
        AvoidObstacle avoid = new AvoidObstacle();

        avoid.character = this;
        avoid.target    = myCohereTarget;
        avoid.flee      = false;
        BlendedSteering prioritySteering = new BlendedSteering();

        prioritySteering.behaviors             = new WeightedBehavior[1];
        prioritySteering.behaviors[0]          = new WeightedBehavior();
        prioritySteering.behaviors[0].behavior = avoid;
        prioritySteering.behaviors[0].weight   = 1f;

        advancedSteering             = new PrioritySteering();
        advancedSteering.myGroups    = new BlendedSteering[2];
        advancedSteering.myGroups[0] = new BlendedSteering();
        advancedSteering.myGroups[0] = prioritySteering;
        advancedSteering.myGroups[1] = new BlendedSteering();
        advancedSteering.myGroups[1] = blendedSteering;
    }
Ejemplo n.º 29
0
    void Start()
    {
        ///Setup behaviours and BlendSteer for general flocking
        #region General Flocking

        ///Start Separation
        //Separate from others and not self
        Separation _separate = new Separation();
        _separate.character = this;
        GameObject[] _goBirds = GameObject.FindGameObjectsWithTag("birds");
        relevantBirds = new GameObject[_goBirds.Length - 1];


        int _relevantBirdCount = 0;
        for (int _bird = 0; _bird < _goBirds.Length - 1; _bird++)
        {
            if (_goBirds[_bird] == this)
            {
                //Ignore self for separation
                continue;
            }
            ///Else separate from them
            relevantBirds[_relevantBirdCount++] = _goBirds[_bird];
        }
        _separate.neighborhood = relevantBirds;
        ///End Separation

        ///Start Cohere
        Arrive _cohere = new Arrive();
        _cohere.character = this;
        _cohere.target    = flockCoMTarget;
        ///End Cohere

        ///Start LWYG
        LookWhereGoing _lWYG = new LookWhereGoing();
        _lWYG.character = this;
        ///End LWYG



        ///Start Blending for general flocking
        _generalFlockSteering            = new BlendedSteering();
        _generalFlockSteering.behaviours = new BehaviourAndWeight[3];

        //Separation
        _generalFlockSteering.behaviours[0]           = new BehaviourAndWeight();
        _generalFlockSteering.behaviours[0].behaviour = _separate;
        _generalFlockSteering.behaviours[0].weight    = 1f; //Weights are still not great, but functional

        //Cohere
        _generalFlockSteering.behaviours[1]           = new BehaviourAndWeight();
        _generalFlockSteering.behaviours[1].behaviour = _cohere;
        _generalFlockSteering.behaviours[1].weight    = .4f;

        //Look Where Going
        _generalFlockSteering.behaviours[2]           = new BehaviourAndWeight();
        _generalFlockSteering.behaviours[2].behaviour = _lWYG;
        _generalFlockSteering.behaviours[2].weight    = 20f;
        ///End Blending

        #endregion General Flocking



        #region Obst Avoid
        ///Start ObstAvoid
        ObstAvoid _obstAvoid = new ObstAvoid();
        _obstAvoid.character     = this;
        _obstAvoid.lookAheadDist = 5f;
        //_obstAvoid.target = flockCoMTarget;

        ///End ObstAvoid

        ///Start Blending for obstacle avoidance
        _obstAvoidSteering            = new BlendedSteering();
        _obstAvoidSteering.behaviours = new BehaviourAndWeight[1];

        //ObstAvoid
        _obstAvoidSteering.behaviours[0]           = new BehaviourAndWeight();
        _obstAvoidSteering.behaviours[0].behaviour = _obstAvoid;
        _obstAvoidSteering.behaviours[0].weight    = 1f; //Priority handles this
        ///End Blending

        #endregion Obst Avoid



        #region Priority Blending
        ///Start Prioritization
        _prioritySteering        = new PrioritySteering();
        _prioritySteering.blends = new BlendedSteering[2];

        //General Flocking
        _prioritySteering.blends[0] = new BlendedSteering();
        _prioritySteering.blends[0] = _obstAvoidSteering;

        //Obst Avoid
        _prioritySteering.blends[1] = new BlendedSteering();
        _prioritySteering.blends[1] = _generalFlockSteering;
        ///End Prioritization

        #endregion Priority Blending
    }
    void Start()
    {
        steering = new PrioritySteering();
        //Make 2 groups, one for flocking, and one for avoiding obstacles
        steering.groups = new BlendedSteering[2];
        //Initialize them
        steering.groups[0] = new BlendedSteering();
        steering.groups[1] = new BlendedSteering();

        //Put obstacle avoidance into its own group
        ObstacleAvoidance avoidAI = new ObstacleAvoidance();

        avoidAI.character = GetComponent <Kinematic>();

        //Initialize array, only 1 behavior needed in this group's consideration
        steering.groups[0].behaviors    = new BehaviorAndWeight[1];
        steering.groups[0].behaviors[0] = new BehaviorAndWeight();

        //Assign specific behavior
        steering.groups[0].behaviors[0].behavior = avoidAI;

        //Set weight
        steering.groups[0].behaviors[0].weight = 4f;

        //Create behaviors to go into the bird
        Arrive arriveAI = new Arrive();

        arriveAI.character = GetComponent <Kinematic>();
        arriveAI.target    = arriveTarget;

        Seperation seperateAI = new Seperation();

        seperateAI.character = GetComponent <Kinematic>();
        GameObject[] allBirds = GameObject.FindGameObjectsWithTag("bird");
        Kinematic[]  allOtherBirdKinematics = new Kinematic[allBirds.Length];
        int          j = 0;

        foreach (GameObject bird in allBirds)
        {
            if (bird != this)
            {
                allOtherBirdKinematics[j] = bird.GetComponent <Kinematic>();
                j++;
            }
            else
            {
                Debug.Log("caught myself");
            }
        }
        seperateAI.targets = allOtherBirdKinematics;

        LookWhereGoing lookAI = new LookWhereGoing();

        lookAI.character = GetComponent <Kinematic>();

        //Set up flocking group in priority steering
        steering.groups[1].behaviors    = new BehaviorAndWeight[3];
        steering.groups[1].behaviors[0] = new BehaviorAndWeight();
        steering.groups[1].behaviors[1] = new BehaviorAndWeight();
        steering.groups[1].behaviors[2] = new BehaviorAndWeight();

        //Assign specific behaviors
        steering.groups[1].behaviors[0].behavior = arriveAI;
        steering.groups[1].behaviors[1].behavior = seperateAI;
        steering.groups[1].behaviors[2].behavior = lookAI;

        //Set weight of each behavior
        steering.groups[1].behaviors[0].weight = 1f;
        steering.groups[1].behaviors[1].weight = 20f;
        steering.groups[1].behaviors[2].weight = 4f;
    }