そろそろ敵ユニット(タレット)を作成し、プレイヤーを攻撃させてみようと思う。
攻撃によるダメージ判定処理は過去記事の通り実装済み
弾でダメージを与える
この処理をプレイヤーにも追加すればお互い攻撃でHPが減るようになる。
なので今回は
・タレットの動作制御(砲塔回転,射撃命令)
・タレットの射撃処理(弾丸の発射)
のスクリプトを作成する。
タレットの動作制御
タレットの動作制御として以下を実現する。
・目標角度まで一定速度以下で旋回を行う。
・目標角度はプレイヤーの偏差射撃位置とする。
・プレイヤーが一定角度以内かつ射程距離内に入った場合は射撃命令(武器クラスに)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Turret : MonoBehaviour { private GameObject target; // Use this for initialization public float speed = 1f; public bool shootingTypeIsDeviation = true; void Start () { target = GameObject.FindGameObjectWithTag("Player"); } // Update is called once per frame void Update () { /*砲塔を回転させる処理*/ Vector3 targetDir; float dis = Vector3.Distance(this.transform.position,target.transform.position); if (shootingTypeIsDeviation) { /*偏差射撃位置を目標値にする*/ float arrTime = dis / gameObject.GetComponent<EnemyWeapon>().GetSpeed(); Vector3 estimationPos = target.transform.position + target.GetComponent<Rigidbody>().velocity * arrTime; targetDir = estimationPos - transform.position; } else { /*現在位置を目標値にする*/ targetDir = target.transform.position - transform.position; } float step = speed * Time.deltaTime; Vector3 newDir = Vector3.RotateTowards(transform.forward, targetDir, step, 10.0F); transform.rotation = Quaternion.LookRotation(newDir); /*射撃するかの判定*/ float angle = Vector3.Angle(transform.forward, targetDir); if (angle < 1) { float range = gameObject.GetComponent<EnemyWeapon> ().GetRange (); if (dis < range + 20) { gameObject.GetComponent<EnemyWeapon> ().SetFire (true); } else { gameObject.GetComponent<EnemyWeapon> ().SetFire (false); } } } } |
一定速度以下で目標角度まで旋回する処理はVector3.RotateTowards関数を用いて実現した。詳細は以下。
でオブジェクトを回転させる方法まとめUnityでオブジェクトを回転させる方法まとめ | tama-lab回転方法はいったん「指定した角度までなめらかに回転させたい時に便利な方法」「指定した角度まで一気に回転させたい時に便利な方法」「ずっと回し続けたいときに便利な方法」「その他の方法」の4つに分類しています。 環境 Unity 5.6.0f3
タレットの射撃処理
タレットの射撃処理として以下を実現する。
・射撃命令により弾丸の発射。
・発射後、次弾発射までクールタイムを設ける。
・タレットの動作制御に必要な射撃情報を渡す。
過去に実装したプレイヤー用の武器スクリプトを流用しながら作成。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
using System.Collections; using System.Collections.Generic; using UnityEngine; public class EnemyWeapon : MonoBehaviour { public GameObject bullet; public int rateOfFire = 60; private int delayTime = 0; public float speed = 100; public float damage = 100; public int effectiveTime = 30; private bool fire = false; // Use this for initialization void Start () { } // Update is called once per frame void Update () { if (fire) { if (delayTime <= 0) { delayTime = rateOfFire; //弾を出現させる位置を取得 Vector3 placePosition = this.transform.position; //出現させる位置をずらす値 Vector3 offsetGun = new Vector3 (0, 0, 3); //武器の向きに合わせて弾の向きも調整 Quaternion q1 = this.transform.rotation; //弾を90度回転させる処理 Quaternion q2 = Quaternion.AngleAxis (90, new Vector3 (1, 0, 0)); Quaternion q = q1 * q2; //弾を出現させる位置を調整 placePosition = q1 * offsetGun + placePosition; //弾生成! GameObject tmpBullet = Instantiate (bullet, placePosition, q) as GameObject; Bullet b = tmpBullet.GetComponent<Bullet> (); b.Create (damage, speed, effectiveTime); } } if (0 < delayTime) { delayTime--; } } public float GetSpeed(){ return speed; } public void SetFire(bool b){ fire = b; } public float GetRange(){ return speed * effectiveTime / 60; } } |
あとはプレイヤーとロックオン対象のHPをUIに追加するなど細々とした部分を作成しました。
そんで以下のようになりました。
以上です。
コメント