//---------------------------------------------------------------------------------------------- // 更新処理 //---------------------------------------------------------------------------------------------- public override void OnUpdate(AutoPilot ap) { if (ap.CheckEnemy() == false && (aimMode == 3 || aimMode == 4)) { ap.SearchEnemy(); } enemyHeight = ap.GetEnemyPosition().y; selfHeight = ap.GetPosition().y; if (ap.CheckEnemy() == true) { friendFlg = false; newHeight = Mathf.Abs(enemyHeight - selfHeight); } //カウント if (count < 3000) { count = count + 1; } else { count = 0; } if (count % 20 == 0) { random = Random.Range(0, 1000); } //ターゲットリセット ap.SearchThreat(MASK_ALL, 1000f); if (count % 600 == 0) { ap.SetCounterSearch(500); } float enemyDistance = ap.GetEnemyDistance(); float enemyAngleR = ap.GetEnemyAngleR(); float enemyAngleU = ap.GetEnemyAngleU(); Vector3 enemyVelocity = ap.GetEnemyVelocity() - ap.GetVelocity(); //味方との連携 int friendDistance = ap.GetFriendDistance(); if (!ap.CheckFriend() && friendFlg) { ap.SearchFriend(""); } if (friendDistance < 200 || !ap.CheckFriend() || ap.CheckEnemy() || !friendFlg) { moveStart = false; sampleDis = 0; checker = 0; } //壁検知 int wallZ = ap.MeasureClearanceToWall(0, -5, 100, 25); int wallXL = ap.MeasureClearanceToWall(100, -5, 0, 10); int wallXR = ap.MeasureClearanceToWall(-100, -5, 0, 10); if (wallZ < 25 || wallXL < 10 || wallXR < 10) { wallCheck = true; } else { wallCheck = false; } // 移動による探索 int random2 = random / 10; if (!ap.CheckEnemy() && !moveStart && !wallCheck) { //ランダム探索 searchAction = "Random"; if (random2 < 20) { ap.StartAction("s", 1); } else if (random2 < 25) { ap.StartAction("a", 1); } else if (random2 < 30) { ap.StartAction("d", 1); } else { ap.StartAction("w", 1); } } else if (!ap.CheckEnemy() && friendDistance >= 50 && ap.CheckFriend() && friendFlg && !wallCheck) { searchAction = "MoveToFriend"; //味方と合流 int ang = ap.GetFriendAngleR(); if (ang > 30) { ap.StartAction("d", 1); } else if (ang < -30) { ap.StartAction("a", 1); } else { ap.StartAction("w", 1); } } else if (ap.CheckEnemy() && enemyDistance <= 50 && !wallCheck) { //回避機動 searchAction = "Back"; if (random2 < 70) { ap.StartAction("w", 1); } else if (random2 > 65) { ap.StartAction("a", 1); } else if (random2 > 60) { ap.StartAction("d", 1); } else { ap.StartAction("s", 1); } } else if (wallCheck) { //壁回避機動 searchAction = "AvoidWall"; if (random2 < 20 || wallXR < 10) { ap.StartAction("d", 1); } else if (random2 < 40 || wallXL < 10) { ap.StartAction("a", 1); } else if (random2 < 60) { ap.StartAction("d", 1); } else if (random2 < 80) { ap.StartAction("a", 1); } else { ap.StartAction("w", 1); } } else { //通常交戦機動 searchAction = "Battle"; if (random2 < 20) { ap.StartAction("s", 1); } else if (random2 < 25) { ap.StartAction("a", 1); } else if (random2 > 60) { ap.StartAction("w", 1); } else if (random2 > 55) { ap.StartAction("d", 1); } } //ランダムジャンプ if (count % 20 == 0 && random % 10 == 0) { ap.StartAction("上昇", 10); } //AI操縦 if (autoPilot && ap.CheckEnemy()) { //武装選択 if (enemyDistance < 100 && random % 3 == 0) { attackAction = "Beamer"; } else if (enemyDistance < 100 && random % 3 == 1) { attackAction = "Cannon"; } else if (enemyDistance < 100 && random % 3 == 2) { attackAction = "Escape"; } else if (enemyDistance < 200 && random % 3 == 0) { attackAction = "Escape"; } else if (enemyDistance < 200 && random % 3 != 0) { attackAction = "Cannon"; } else if (random % 3 == 0) { attackAction = "Escape"; } else if (random % 3 == 1) { attackAction = "Cannon"; } else if (random % 3 == 2) { attackAction = "Missile"; } else { attackAction = "Cannon"; } } else { attackAction = "Escape"; } //対地対空機銃自動エイミングモード if (count % 100 == 10) { autoAim = true; if (newHeight > heightSwitch) { aimMode = 4; ap.StartAction("AIM1", -1); ap.EndAction("AIM2"); } else { aimMode = 3; ap.StartAction("AIM2", -1); ap.EndAction("AIM1"); } } if (newHeight > heightSwitch && oldHeight <= heightSwitch && autoAim) { aimMode = 4; ap.StartAction("AIM1", -1); ap.EndAction("AIM2"); } if (newHeight < heightSwitch && oldHeight >= heightSwitch && autoAim) { aimMode = 3; ap.StartAction("AIM2", -1); ap.EndAction("AIM1"); } // 攻撃 int energy = ap.GetEnergy(); if (energy > 10 && (attackAction == "Cannon")) { ap.StartAction("ATK1", -1); } else { ap.EndAction("ATK1"); } if (energy > 15 && (attackAction == "Beamer")) { ap.StartAction("ATK2", -1); } else { ap.EndAction("ATK2"); } if (energy > 5 && (attackAction == "Missile")) { ap.StartAction("ATK3", -1); chargeCount++; } else if (energy > 5 && chargeCount > 0 && chargeCount < 2000) { chargeCount++; } else { chargeCount = 0; ap.EndAction("ATK3"); } //情報表示 ap.Print(0, "Enemy : " + ap.GetEnemyName()); ap.Print(1, "Distance : " + enemyDistance); ap.Print(2, "AngleR : " + ap.GetEnemyAngleR()); ap.Print(3, "AimMode : " + aimMode); ap.Print(4, "EnemyHeight : " + ap.GetEnemyPosition().y); ap.Print(5, "SelfHeight : " + ap.GetPosition().y); ap.Print(6, "Count : " + count); ap.Print(7, "Weapon : " + attackAction); ap.Print(8, "Search : " + searchAction); ap.Print(9, "WallCheck : " + wallCheck); // エイム float ed = enemyDistance * 0.003f; Vector3 mv = ap.MulVec(enemyVelocity, ed); if (aimMode == 3 || aimMode == 4) { //機銃オートエイム Vector3 estPos = ap.AddVec(ap.GetEnemyPosition(), mv); ap.Aim(estPos); } oldHeight = newHeight; }
//---------------------------------------------------------------------------------------------- // Update //---------------------------------------------------------------------------------------------- public override void OnUpdate(AutoPilot ap) { // Attack int energy = ap.GetEnergy(); if (energy > 20 && Input.GetMouseButton(0)) { ap.StartAction("ATK1", -1); } else { ap.EndAction("ATK1"); } if (Input.GetMouseButtonDown(2)) { if (energy > 10 && !sword) { ap.StartAction("ATK3", -1); sword = true; } else { ap.EndAction("ATK3"); sword = false; } } if (energy > 10 && jetStriker) { ap.StartAction("ATK3", 1); } if (energy <= 10) { ap.EndAction("ATK3"); sword = false; } if (energy > 10 && Input.GetMouseButtonDown(1) && !missile) { ap.StartAction("ATK2", -1); missile = true; } else if (Input.GetMouseButtonDown(1) || energy <= 10) { ap.EndAction("ATK2"); missile = false; } //Move if (!flightMode) { //Human Mode if (Input.GetKey(KeyCode.W)) { ap.StartAction("w", 1); } if (Input.GetKey(KeyCode.A)) { ap.StartAction("a", 1); } if (Input.GetKey(KeyCode.S)) { ap.StartAction("s", 1); } if (Input.GetKey(KeyCode.D)) { ap.StartAction("d", 1); } if (Input.GetKey(KeyCode.Space)) { ap.StartAction("up", 1); } if (Input.GetKey(KeyCode.F)) { ap.StartAction("down", 1); } if ((Input.GetKeyDown(KeyCode.Alpha1) && aim) || Input.GetKeyDown(KeyCode.Q)) { ap.EndAction("AIM1"); aim = false; } else if (Input.GetKeyDown(KeyCode.Alpha1) && !aim || Input.GetKeyUp(KeyCode.Q)) { ap.StartAction("AIM1", -1); aim = true; } if (Input.GetKeyDown(KeyCode.Alpha2)) { ap.StartAction("change", -1); ap.EndAction("AIM1"); flightMode = true; } if (Input.GetKeyDown(KeyCode.E)) { ap.StartAction("change", -1); ap.EndAction("AIM1"); flightMode = true; autoPilot = true; jetStriker = true; } else { jetStriker = false; } } else { //Fighter Mode if (jetStriker && Input.GetKeyDown(KeyCode.E)) { jetStriker = false; autoPilot = false; } else if (!jetStriker && Input.GetKeyDown(KeyCode.E)) { jetStriker = true; autoPilot = true; ap.ForgetEnemy(); } if (Input.GetKey(KeyCode.W) || jetStriker) { ap.StartAction("FMforth", 1); } if (Input.GetKey(KeyCode.A)) { ap.StartAction("a", 1); } if (Input.GetKey(KeyCode.S)) { ap.StartAction("FMback", 1); } if (Input.GetKey(KeyCode.D)) { ap.StartAction("d", 1); } if (Input.GetKey(KeyCode.Space)) { ap.StartAction("FMup", 1); } if (Input.GetKey(KeyCode.F)) { ap.StartAction("FMdown", 1); } if (Input.GetKeyDown(KeyCode.Alpha2)) { ap.EndAction("change"); flightMode = false; ap.StartAction("AIM1", -1); aim = true; } } //AutoAim float enemyDistance = ap.GetEnemyDistance(); float enemyAngleR = ap.GetEnemyAngleR(); float enemyAngleU = ap.GetEnemyAngleU(); Vector3 enemyVelocity = ap.GetEnemyVelocity() - ap.GetVelocity(); enemyHeight = ap.GetEnemyPosition().y; selfHeight = ap.GetPosition().y; //AutoPilot if (autoPilot) { if (!ap.CheckEnemy()) { ap.SearchEnemy(); } aim = true; //TargetReset if (Input.GetKeyDown(KeyCode.Alpha4)) { ap.ForgetEnemy(); } ap.SelectEnemy(KeyCode.Alpha4); if (ap.CheckEnemy()) { newHeight = Mathf.Abs(enemyHeight - selfHeight); } else { newHeight = 0; } if (newHeight > heightSwitch && !Input.GetKey(KeyCode.Q)) { aimMode = 1; if (!flightMode) { ap.StartAction("AIM1", -1); if (enemyAngleR < -15 && !Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.A) && !Input.GetKey(KeyCode.S) && !Input.GetKey(KeyCode.D)) { ap.StartAction("a", 1); } if (enemyAngleR > 15 && !Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.A) && !Input.GetKey(KeyCode.S) && !Input.GetKey(KeyCode.D)) { ap.StartAction("d", 1); } } else { if (enemyAngleR < -5 && !Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.A) && !Input.GetKey(KeyCode.S) && !Input.GetKey(KeyCode.D)) { ap.StartAction("a", 1); } if (enemyAngleR > 5 && !Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.A) && !Input.GetKey(KeyCode.S) && !Input.GetKey(KeyCode.D)) { ap.StartAction("d", 1); } if (enemyHeight - selfHeight > heightSwitch && !Input.GetKey(KeyCode.Space) && !Input.GetKey(KeyCode.F)) { ap.StartAction("FMup", 1); } if (enemyHeight - selfHeight < -heightSwitch && !Input.GetKey(KeyCode.Space) && !Input.GetKey(KeyCode.F)) { ap.StartAction("FMdown", 1); } } } else if (!Input.GetKey(KeyCode.Q)) { aimMode = 2; if (!flightMode) { ap.StartAction("AIM1", -1); if (enemyAngleR < -15 && !Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.A) && !Input.GetKey(KeyCode.S) && !Input.GetKey(KeyCode.D)) { ap.StartAction("a", 1); } if (enemyAngleR > 15 && !Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.A) && !Input.GetKey(KeyCode.S) && !Input.GetKey(KeyCode.D)) { ap.StartAction("d", 1); } } else { if (enemyAngleR < -5 && !Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.A) && !Input.GetKey(KeyCode.S) && !Input.GetKey(KeyCode.D)) { ap.StartAction("a", 1); } if (enemyAngleR > 5 && !Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.A) && !Input.GetKey(KeyCode.S) && !Input.GetKey(KeyCode.D)) { ap.StartAction("d", 1); } if (enemyHeight - selfHeight < heightSwitch && enemyHeight - selfHeight > -heightSwitch && !Input.GetKey(KeyCode.Space) && !Input.GetKey(KeyCode.F)) { if (ap.GetVelocity().y < -10) { ap.StartAction("FMup", 1); } if (ap.GetVelocity().y > 10) { ap.StartAction("FMdown", 1); } } } } else { aimMode = 0; ap.EndAction("AIM1"); } if (Input.GetKeyDown(KeyCode.Alpha3) || (Input.GetKeyDown(KeyCode.Alpha1) && aim)) { ap.ForgetEnemy(); autoPilot = false; jetStriker = false; if (!flightMode) { ap.StartAction("AIM1", -1); } } if (aimMode == 1 || aimMode == 2) { // Aim & Attack Vector3 ev = ap.GetEnemyVelocity() - ap.GetVelocity(); float ed = enemyDistance * 0.003f; Vector3 mv = ap.MulVec(ev, ed); //AutoAim Vector3 estPos = ap.AddVec(ap.GetEnemyPosition(), mv); ap.Aim(estPos); } } else { aimMode = 0; if (Input.GetKeyDown(KeyCode.Alpha3)) { autoPilot = true; } } if (friendFlg) { if (!ap.CheckFriend()) { ap.SearchFriend(""); } if (!ap.CheckEnemy() && ap.CheckFriend()) { searchAction = "SearchFriend"; int ang = ap.GetFriendAngleR(); if (ang > 15) { ap.StartAction("d", 1); } else if (ang < -15) { ap.StartAction("a", 1); } friendCount++; if (Input.GetKeyDown(KeyCode.Alpha5) || friendCount > 199) { friendFlg = false; friendCount = 0; ap.ForgetFriend(); searchAction = "Standby"; } } } else { if (Input.GetKeyDown(KeyCode.Alpha5)) { friendFlg = true; searchAction = "SearchFriend"; } } //AGD if (flightMode && (ap.GetGroundClearance() > 10 || Input.GetKey(KeyCode.Alpha6))) { ap.StartAction("AGD", 1); } //Display Information ap.Print(0, "Enemy : " + ap.GetEnemyName()); ap.Print(1, "Distance : " + enemyDistance); ap.Print(2, "AngleR : " + ap.GetEnemyAngleR()); ap.Print(3, "AimMode : " + aimMode); ap.Print(4, "EnemyHeight : " + ap.GetEnemyPosition().y); ap.Print(5, "SelfHeight : " + ap.GetPosition().y); ap.Print(6, "NewHeight : " + newHeight); ap.Print(7, "SearchAction : " + searchAction); ap.Print(8, "UpDownVel : " + ap.GetVelocity().y); }
//---------------------------------------------------------------------------------------------- // 更新処理 //---------------------------------------------------------------------------------------------- public override void OnUpdate(AutoPilot ap) { //グリズリーのAIをやっつけで自動化したので余計な変数が多々あります よって解説付き //敵を捕捉していないときに索敵します if (ap.CheckEnemy() == false) { ap.SearchEnemy(); } //敵と味方の高さ(Y軸)を記録 enemyHeight = ap.GetEnemyPosition().y; selfHeight = ap.GetPosition().y; //敵がいるときは友軍との合流をあきらめ(1行目)、自機と敵機の高さの差を測ります(2行目) if (ap.CheckEnemy() == true) { friendFlg = false; newHeight = Mathf.Abs(enemyHeight - selfHeight); } //カウント(一定時間ごとの周期的な動作に利用します) if (count < 3000) { count = count + 1; } else { count = 0; } //20フレームごとに乱数を更新します if (count % 20 == 0) { random = Random.Range(0, 1000); } //ターゲットリセット ap.SearchThreat(MASK_ALL, 1000f); if (count % 600 == 0) { ap.SetCounterSearch(500); } //敵の方向・距離・相対的な移動速度を取得 float enemyDistance = ap.GetEnemyDistance(); float enemyAngleR = ap.GetEnemyAngleR(); float enemyAngleU = ap.GetEnemyAngleU(); Vector3 enemyVelocity = ap.GetEnemyVelocity() - ap.GetVelocity(); //味方との連携用 int friendDistance = ap.GetFriendDistance(); if (!ap.CheckFriend() && friendFlg) { ap.SearchFriend(""); moveStart = true; } if (friendDistance < 200 || !ap.CheckFriend() || ap.CheckEnemy() || !friendFlg) { moveStart = false; } //壁検知 int wallZ = ap.MeasureClearanceToWall(0, -5, 100, 25); int wallXL = ap.MeasureClearanceToWall(100, -5, 0, 10); int wallXR = ap.MeasureClearanceToWall(-100, -5, 0, 10); if (wallZ < 25 || wallXL < 10 || wallXR < 10) { wallCheck = true; } else { wallCheck = false; } // 移動方法 int random2 = random / 10; if (!ap.CheckEnemy() && !moveStart && !wallCheck) { //ランダム探索 searchAction = "Random"; if (random2 < 20) { ap.StartAction("s", 1); } else if (random2 < 25) { ap.StartAction("a", 1); } else if (random2 < 30) { ap.StartAction("d", 1); } else { ap.StartAction("w", 1); } } else if (!ap.CheckEnemy() && friendDistance >= 50 && ap.CheckFriend() && friendFlg && !wallCheck) { searchAction = "MoveToFriend"; //味方と合流 int ang = ap.GetFriendAngleR(); if (ang > 30) { ap.StartAction("d", 1); } else if (ang < -30) { ap.StartAction("a", 1); } else { ap.StartAction("w", 1); } } else if (ap.CheckEnemy() && enemyDistance <= 50 && !wallCheck) { //回避機動 searchAction = "Back"; if (random2 < 70) { ap.StartAction("w", 1); } else if (random2 > 65) { ap.StartAction("a", 1); } else if (random2 > 60) { ap.StartAction("d", 1); } else { ap.StartAction("s", 1); } } else if (wallCheck) { //壁回避機動 searchAction = "AvoidWall"; if (random2 < 20 || wallXR < 10) { ap.StartAction("d", 1); } else if (random2 < 40 || wallXL < 10) { ap.StartAction("a", 1); } else if (random2 < 60) { ap.StartAction("d", 1); } else if (random2 < 80) { ap.StartAction("a", 1); } else { ap.StartAction("w", 1); } } else { //通常交戦機動 searchAction = "Battle"; if (random2 < 20) { ap.StartAction("s", 1); } else if (random2 < 25) { ap.StartAction("a", 1); } else if (random2 > 60) { ap.StartAction("w", 1); } else if (random2 > 55) { ap.StartAction("d", 1); } } //ランダムにジャンプ if (count % 20 == 0 && random % 10 == 0) { ap.StartAction("上昇", 10); } //AI操縦時の武装選択 if (autoPilot && ap.CheckEnemy()) { if (enemyDistance < 100 && random % 3 == 0) { attackAction = "Beamer"; } else if (enemyDistance < 100 && random % 3 == 1) { attackAction = "Cannon"; } else if (enemyDistance < 100 && random % 3 == 2) { attackAction = "Escape"; } else if (enemyDistance < 200 && random % 3 == 0) { attackAction = "Escape"; } else if (enemyDistance < 200 && random % 3 != 0) { attackAction = "Cannon"; } else if (random % 3 == 0) { attackAction = "Escape"; } else if (random % 3 == 1) { attackAction = "Cannon"; } else if (random % 3 == 2) { attackAction = "Missile"; } else { attackAction = "Cannon"; } } else { attackAction = "Escape"; } //対地・対空エイミングモード自動切替 if (newHeight > heightSwitch && autoAim) { aimMode = 4; ap.StartAction("AIM1", 1); ap.EndAction("AIM2"); } if (newHeight <= heightSwitch && autoAim) { aimMode = 3; ap.StartAction("AIM2", 1); ap.EndAction("AIM1"); } //自動攻撃(エネルギー切れ防止機構付き) int energy = ap.GetEnergy(); if (energy > 10 && (attackAction == "Cannon")) { ap.StartAction("ATK1", -1); } else { ap.EndAction("ATK1"); } if (energy > 15 && (attackAction == "Beamer")) { ap.StartAction("ATK2", -1); } else { ap.EndAction("ATK2"); } if (energy > 5 && (attackAction == "Missile")) { ap.StartAction("ATK3", -1); chargeCount++; } else if (energy > 5 && chargeCount > 0 && chargeCount < 2000) { chargeCount++; } else { chargeCount = 0; ap.EndAction("ATK3"); } //情報表示 ap.Print(0, "Enemy : " + ap.GetEnemyName()); ap.Print(1, "Distance : " + enemyDistance); ap.Print(2, "AngleR : " + ap.GetEnemyAngleR()); ap.Print(3, "AimMode : " + aimMode); ap.Print(4, "EnemyHeight : " + ap.GetEnemyPosition().y); ap.Print(5, "SelfHeight : " + ap.GetPosition().y); ap.Print(6, "Count : " + count); ap.Print(7, "Weapon : " + attackAction); ap.Print(8, "Search : " + searchAction); ap.Print(9, "WallCheck : " + wallCheck); //エイム用データ取得 float ed = enemyDistance * 0.003f; Vector3 mv = ap.MulVec(enemyVelocity, ed); Vector3 estPos = ap.AddVec(ap.GetEnemyPosition(), mv); ap.Aim(estPos); }