Exemplo n.º 1
0
    // Update is called once per frame
    void Update()
    {
        // 加速度(有碰撞 就避免碰撞)
        Vector3 accel = colAvoid.GetSteering(colAvoidSensor.targets);

        // 没有任何碰撞的时候就  漫游
        if (accel.magnitude < 0.005f)
        {
            accel = wander.GetSteering();
        }

        // 速度
        steeringBasics.Steer(accel);
        // 朝向
        steeringBasics.LookWhereYoureGoing();
    }
Exemplo n.º 2
0
    void Update()
    {
        // 调试
        path.draw();

        // 是否原路返回
        if (isAtEndOfPath())
        {
            path.reversePath();
        }

        // 躲避 加速度
        Vector3 accel = colAvoid.GetSteering(colAvoidSensor.targets);

        // 不需要躲避 就沿着路径走
        if (accel.magnitude < 0.005f)
        {
            accel = followPath.getSteering(path);
        }

        // 设置刚体速度 和 朝向
        steeringBasics.Steer(accel);
        steeringBasics.LookWhereYoureGoing();
    }
    // Update is called once per frame
    void Update()
    {
        if (float.IsNaN(angularVel))
        {
            angularVel = 0.0f;
        }

        SteeringOutput moving  = new SteeringOutput();
        SteeringOutput turning = new SteeringOutput();

        seeking.character = this;
        seeking.target    = aiTarget;

        SteeringOutput movement = new SteeringOutput();

        switch (movementType)
        {
        case "Seek":
            moving = seeking.GetSteering();

            if (moving == null)
            {
                movement.linear = Vector3.zero;
            }
            else
            {
                movement.linear  = moving.linear;
                movement.angular = turning.angular;
            }

            break;

        case "Flee":
            flee = true;

            moving = seeking.GetSteering();

            if (moving == null)
            {
                movement.linear = Vector3.zero;
            }
            else
            {
                movement.linear  = moving.linear;
                movement.angular = turning.angular;
            }

            break;

        case "Arrive":
            moving = arrive.GetSteering();

            if (moving == null)
            {
                movement.linear = Vector3.zero;
            }
            else
            {
                movement.linear  = moving.linear;
                movement.angular = turning.angular;
            }

            break;

        case "Face":

            facing.target    = aiTarget;
            facing.character = this;

            //moving = seeking.GetSteering();
            turning = facing.GetSteering();

            movement.linear  = Vector3.zero;
            movement.angular = turning.angular;

            break;

        case "Seek Align":

            aligned.character = this;
            aligned.target    = aiTarget;

            moving  = seeking.GetSteering();
            turning = aligned.GetSteering();

            movement.linear  = moving.linear;
            movement.angular = turning.angular;

            break;

        case "Seek Face":
            facing.character = this;
            facing.target    = aiTarget;

            moving  = seeking.GetSteering();
            turning = facing.GetSteering();

            if (moving == null)
            {
                movement.linear = Vector3.zero;
            }
            else
            {
                movement.linear  = moving.linear;
                movement.angular = turning.angular;
            }

            break;

        case "Seek Look":

            looky.character = this;
            looky.target    = aiTarget;

            //Debug.Log(looky.GetSteering());
            if (looky.GetSteering() == null)
            {
                moving           = seeking.GetSteering();
                movement.linear  = moving.linear;
                movement.angular = 0;
            }
            else
            {
                moving  = seeking.GetSteering();
                turning = looky.GetSteering();

                //Debug.Log(looky.GetSteering());

                movement.linear  = moving.linear;
                movement.angular = turning.angular;
            }

            break;

        case "Arrive Align":

            aligned.character = this;
            aligned.target    = aiTarget;


            moving  = arrive.GetSteering();
            turning = aligned.GetSteering();

            if (moving == null)
            {
                movement.linear = Vector3.zero;
            }
            else
            {
                movement.linear  = moving.linear;
                movement.angular = turning.angular;
            }

            break;

        case "Arrive Face":

            facing.character = this;
            facing.target    = aiTarget;

            moving  = arrive.GetSteering();
            turning = facing.GetSteering();

            movement.linear  = moving.linear;
            movement.angular = turning.angular;

            break;

        case "Arrive Look":

            looky.character = this;
            //looky.target = aiTarget;

            arrive.character = this;
            arrive.target    = aiTarget;

            moving  = arrive.GetSteering();
            turning = looky.GetSteering();

            movement.linear  = moving.linear;
            movement.angular = turning.angular;

            Debug.Log(movement.angular);

            break;

        case "Path Follow":

            pathFollow.path      = aiTargets;
            pathFollow.character = this;

            pathFollow.maxAccel = maxAccel;
            pathFollow.maxSpeed = maxSpeed;

            pathFollow.targetRad = targetRad;
            pathFollow.slowRad   = slowRad;

            moving = pathFollow.GetSteering();

            movement.linear = moving.linear;

            break;

        case "Path Follow Face":

            pathFollow.path      = aiTargets;
            pathFollow.character = this;

            pathFollow.maxAccel = maxAccel;
            pathFollow.maxSpeed = maxSpeed;

            pathFollow.targetRad = targetRad;
            pathFollow.slowRad   = slowRad;

            moving = pathFollow.GetSteering();

            aiTarget = pathFollow.target;

            facing.character = this;
            facing.target    = aiTarget;

            turning = facing.GetSteering();

            movement.linear  = moving.linear;
            movement.angular = turning.angular;

            break;

        case "Separate":

            separate.character = this;
            separate.avoid     = kinTargets;

            separate.threshold = threshold;
            separate.decay     = decay;

            //separate.avoid = avoidance;

            moving = separate.GetSteering();

            movement.linear  = moving.linear;
            movement.angular = 0;

            break;

        case "Pursue":

            pursuing.character = this;
            pursuing.target    = aiTarget;

            moving = pursuing.GetSteering();

            movement.linear  = moving.linear;
            movement.angular = 0;

            break;

        case "Avoidance":
            avoid.character = this;
            avoid.targets   = kinTargets;

            kinTarget = avoid.firstTarget;

            moving = avoid.GetSteering();

            movement.linear = moving.linear;

            movement.angular = 0;

            break;

        case "Obstacles":

            obstacles.character = this;
            //obstacles.target = aiTarget;

            looky.character = this;
            looky.target    = obstacles.target;

            moving  = obstacles.GetSteering();
            turning = looky.GetSteering();

            movement.linear  = moving.linear;
            movement.angular = turning.angular;

            break;

        case "Flocking":

            arrive.character = this;
            arrive.target    = aiTarget;

            arrive.maxAccel = maxAccel;
            arrive.maxSpeed = maxSpeed;

            arrive.targetRad = targetRad;
            arrive.slowRad   = slowRad;

            looky.character = this;

            obstacles.character = this;

            separate.character = this;
            separate.avoid     = kinTargets;

            separate.threshold = threshold;
            separate.decay     = decay;

            behaviors[0] = new BehaviorAndWeight();

            behaviors[0].behavior = arrive;
            behaviors[0].weight   = 35f;

            behaviors[1] = new BehaviorAndWeight();

            behaviors[1].behavior = separate;
            behaviors[1].weight   = 1f;

            behaviors[2] = new BehaviorAndWeight();

            behaviors[2].behavior = looky;
            behaviors[2].weight   = 50f;

            behaviors[3] = new BehaviorAndWeight();

            behaviors[3].behavior = obstacles;
            behaviors[3].weight   = 10f;

            flocking.behaviors = behaviors;

            flocking.maxAccel    = maxAccel;
            flocking.maxRotation = maxRot;

            moving = flocking.GetSteering();

            movement.linear  = moving.linear;
            movement.angular = moving.angular;

            break;

        default:

            facing.character = this;
            facing.target    = aiTarget;


            moving  = seeking.GetSteering();
            turning = aligned.GetSteering();

            if (moving == null)
            {
                movement.linear = Vector3.zero;
            }
            else
            {
                movement.linear  = moving.linear;
                movement.angular = turning.angular;
            }

            break;
        }

        // Update linear and angular velocities

        //Debug.Log(movement.angular);

        linearVel  += movement.linear * Time.deltaTime;
        angularVel += movement.angular * Time.deltaTime;

        //angularInc *= Mathf.Rad2Deg;

        transform.position += linearVel * Time.deltaTime * maxSpeed;

        angularInc = new Vector3(0, angularVel * Time.deltaTime * angVelMax, 0);
        //Debug.Log(angularInc)



        if (!float.IsNaN(angularInc.y))
        {
            transform.eulerAngles += angularInc;
        }

        //Debug.Log(linearVel);

        if (linearVel.magnitude > maxSpeed)
        {
            linearVel.Normalize();
            linearVel *= maxSpeed;
        }
    }
    // 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;
        }
    }
    void Update()
    {
        SteeringOutput movementSteering;

        //SteeringOutput lookSteering;
        //Update position and rotation
        transform.position += linearVelocity * Time.deltaTime;
        Vector3 angularIncrement = new Vector3(0, angularVelocity * Time.deltaTime, 0);

        transform.eulerAngles += angularIncrement;

        switch (moveType)
        {
        case SteeringType.Pursue:
            movementSteering = pursueAI.GetSteering();
            break;

        case SteeringType.Evade:
            movementSteering = evadeAI.GetSteering();
            break;

        case SteeringType.FollowPath:
            movementSteering = followAI.GetSteering();
            break;

        case SteeringType.Seek:
            movementSteering = seekAI.GetSteering();
            break;

        case SteeringType.Flee:
            movementSteering = fleeAI.GetSteering();
            break;

        case SteeringType.Seperation:
            movementSteering = seperationAI.GetSteering();
            break;

        case SteeringType.Arrive:
            movementSteering = arriveAI.GetSteering();
            break;

        case SteeringType.CollisionAvoidance:
            movementSteering = avoidAI.GetSteering();
            break;

        case SteeringType.ObstacleAvoidance:
            movementSteering = obstacleAI.GetSteering();
            break;

        default:
            movementSteering = new SteeringOutput();
            break;
        }

        if (movementSteering != null)
        {
            linearVelocity += movementSteering.linear * Time.deltaTime;
            //angularVelocity += movementSteering.angular * Time.deltaTime;
        }

        switch (lookType)
        {
        case LookType.None:
            break;

        case LookType.Align:
            lookSteering = alignAI.GetSteering();
            break;

        case LookType.Face:
            lookSteering = faceAI.GetSteering();
            break;

        case LookType.LookWhereGoing:
            lookSteering = lookAI.GetSteering();
            break;

        default:
            lookSteering = alignAI.GetSteering();
            break;
        }

        if (lookSteering != null)
        {
            angularVelocity += lookSteering.angular * Time.deltaTime;
        }
        //Update kinematic reference with complex data it can't get by itself
        kinematic.GetData(movementSteering);
        kinematic.GetData(lookSteering);
    }