Esempio n. 1
0
        void Update()
        {
            Vector3 targ = target ? target.transform.position : last_seen_pos;

            //Use memory if no more target
            if (target == null && last_target != null && memory_duration > 0.1f)
            {
                memory_timer += Time.deltaTime;
                if (memory_timer < memory_duration)
                {
                    last_seen_pos = last_target.transform.position;
                    targ          = last_seen_pos;
                }
            }

            //Move to target
            enemy.MoveTo(targ, speed_mult);
            enemy.FaceToward(enemy.GetMoveTarget(), 2f);

            if (target != null)
            {
                last_target   = target;
                last_seen_pos = target.transform.position;
                memory_timer  = 0f;
            }
        }
Esempio n. 2
0
        void Update()
        {
            state_timer += Time.deltaTime;

            //While patroling, detect targets
            if (state == EnemyLOS2DState.Patrol)
            {
                DetectVisionTarget();
                DetectTouchVisionTarget();
            }

            //When just seen the VisionTarget, enemy alerted
            if (state == EnemyLOS2DState.Alert)
            {
                if (seen_character == null)
                {
                    ChangeState(EnemyLOS2DState.Patrol);
                    return;
                }

                bool could_see_target = CouldSeeObject(seen_character.gameObject) || CanTouchObject(seen_character.gameObject);
                if (could_see_target)
                {
                    enemy.FaceToward(seen_character.transform.position);
                }

                if (state_timer > detect_time)
                {
                    bool can_see_target = CanSeeVisionTarget(seen_character) || CanTouchObject(seen_character.gameObject);
                    if (enemy_follow && can_see_target)
                    {
                        ChangeState(EnemyLOS2DState.Chase);
                        enemy_follow.target = seen_character.gameObject;

                        if (onDetectTarget != null)
                        {
                            onDetectTarget.Invoke(seen_character);
                        }
                    }
                }

                if (state_timer > alerted_time)
                {
                    ChangeState(EnemyLOS2DState.Patrol);
                }

                DetectTouchTarget();
            }

            //If seen long enough (detect time), will go into a chase
            if (state == EnemyLOS2DState.Chase)
            {
                if (seen_character == null)
                {
                    ChangeState(EnemyLOS2DState.Patrol);
                    return;
                }

                bool can_see_target = CanSeeVisionTarget(seen_character);
                enemy_follow.target = can_see_target ? seen_character.gameObject : null;

                if (state_timer > follow_time)
                {
                    if (!can_see_target)
                    {
                        ChangeState(EnemyLOS2DState.Patrol);
                    }
                }

                if (enemy_follow.HasReachedTarget() && !can_see_target)
                {
                    ChangeState(EnemyLOS2DState.Confused);
                }

                DetectTouchTarget();
            }

            //After the chase, if VisionTarget is unseen, enemy will be confused
            if (state == EnemyLOS2DState.Confused)
            {
                bool can_see_target = CanSeeVisionTarget(seen_character);
                if (can_see_target)
                {
                    ChangeState(EnemyLOS2DState.Chase);
                }

                if (state_timer > alerted_time)
                {
                    ChangeState(EnemyLOS2DState.Patrol);
                }
            }
        }
Esempio n. 3
0
        void Update()
        {
            wait_timer += Time.deltaTime;

            move_dir = Vector3.right * Mathf.Sign(transform.localScale.x);

            //If still in starting path
            if (!waiting && !HasFallen() && path_list.Count > 0)
            {
                //Move
                Vector3 targ = path_list[current_path];
                enemy.MoveTo(targ, speed_mult);
                move_dir = Vector3.right * Mathf.Sign((targ - transform.position).x);

                //Check if reached target
                Vector3 dist_vect = (targ - transform.position);
                dist_vect.z = 0f;
                if (dist_vect.magnitude < 0.1f)
                {
                    waiting    = true;
                    wait_timer = 0f;
                }

                //Check if obstacle ahead
                bool fronted = enemy.CheckFronted(dist_vect.normalized);
                if (fronted && wait_timer > 2f)
                {
                    RewindPath();
                    wait_timer = 0f;
                }
            }

            //If can't reach starting path anymore
            if (!waiting && HasFallen())
            {
                //Move
                Vector3 mdir = Vector3.right * (path_rewind ? -2f : 2f);
                Vector3 targ = transform.position + mdir;
                enemy.MoveTo(targ, speed_mult);
                enemy.FaceToward(targ);
                move_dir = Vector3.right * Mathf.Sign((targ - transform.position).x);

                //Check if obstacle ahead
                Vector3 dist_vect = (targ - transform.position);
                bool    fronted   = enemy.CheckFronted(dist_vect.normalized);
                if (fronted && wait_timer > 2f)
                {
                    path_rewind = !path_rewind;
                    wait_timer  = 0f;
                }
            }

            if (waiting)
            {
                //Wait a bit
                if (wait_timer > wait_time)
                {
                    GoToNextPath();
                    waiting    = false;
                    wait_timer = 0f;
                }
            }

            //Angle
            pause_timer += Time.deltaTime;
            if (pause_timer > pause_duration)
            {
                float angle_target = angle_rewind ? angle_min : angle_max;
                current_angle = Mathf.MoveTowards(current_angle, angle_target, angle_speed * Time.deltaTime);
                face_dir      = new Vector3(Mathf.Cos(current_angle * Mathf.Deg2Rad), Mathf.Sin(current_angle * Mathf.Deg2Rad), 0f);
                face_dir      = new Vector3(Mathf.Sign(move_dir.x) * face_dir.x, face_dir.y, 0f);
            }
            enemy.FaceToward(transform.position + face_dir * 2f);

            if (!angle_rewind && enemy.GetFacingAngle() > angle_max)
            {
                angle_rewind = true;
                pause_timer  = 0f;
            }
            if (angle_rewind && enemy.GetFacingAngle() < angle_min)
            {
                angle_rewind = false;
                pause_timer  = 0f;
            }
        }