プレイヤーの動きに合わせて回転する敵キャラを作ってみました。
作ってみたゲームはこちら。
プレイヤーの動きに合わせて回転しています。
コードはこちら。
public void OnAttackRangeEnter(Collider other) { //プレイヤーの方向を向く Vector3 target = other.transform.position; target.y = this.transform.position.y; this.transform.LookAt(target); //弾を発射する GameObject bullet = GameObject.Instantiate(bulletPrefab, muzzlePoint.position, muzzlePoint.rotation) as GameObject; bullet.GetComponent<Rigidbody>().AddForce(bullet.transform.forward * shotSpeed); gunSound.Play(); }
一定のエリアに入ってきたら、相手の方向を向いて弾を発射しています。