前提・実現したいこと
今、unityで横スクロールシューティングゲームを作っています。敵の攻撃手段として、ミサイルと通常弾を発射して攻撃するのですが、振り分けているキーを押すと弾が出るようにしているため、この二つの弾が同時に出てしまいます。通常弾をセットしているときはミサイルが撃てない、といった事をする方法が調べてもよくわかりませんでした。この方法を教えていただきたいです。
発生している問題・エラーメッセージ
攻撃手段であるミサイルと通常弾が同時に発射される
該当のソースコード
ソースコード
Playerにアタッチしているscript
using UnityEngine;
using System.Collections;
public class PlayerScript : MonoBehaviour
{
public float speed = 4f; public float jumpPower = 700; public LayerMask groundLayer; public GameObject mainCamera; public GameObject bullet; private Rigidbody2D rigidbody2D; private Animator anim; private bool isGrounded; private Renderer renderer; void Start() { anim = GetComponent<Animator>(); rigidbody2D = GetComponent<Rigidbody2D>(); renderer = GetComponent<Renderer>(); } void Update() { isGrounded = Physics2D.Linecast( transform.position + transform.up * 1, transform.position - transform.up * 0.05f, groundLayer); if (Input.GetKeyDown("left shift")) { if (isGrounded) { anim.SetBool("Dash", false); anim.SetTrigger("Jump"); isGrounded = false; rigidbody2D.AddForce(Vector2.up * jumpPower); } } float velY = rigidbody2D.velocity.y; bool isJumping = velY > 0.1f ? true : false; bool isFalling = velY < -0.1f ? true : false; anim.SetBool("isJumping", isJumping); anim.SetBool("isFalling", isFalling); if (Input.GetKeyDown("space")) { anim.SetTrigger("Shot"); Instantiate(bullet, transform.position + new Vector3(0.4f, 1.2f, 0f), transform.rotation); // ショット音を鳴らす GetComponent<AudioSource>().Play(); } } void FixedUpdate() { float x = Input.GetAxisRaw("Horizontal"); if (x != 0) { rigidbody2D.velocity = new Vector2(x * speed, rigidbody2D.velocity.y); Vector2 temp = transform.localScale; temp.x = x; transform.localScale = temp; anim.SetBool("Dash", true); if (transform.position.x > mainCamera.transform.position.x - 4) { Vector3 cameraPos = mainCamera.transform.position; cameraPos.x = transform.position.x + 4; mainCamera.transform.position = cameraPos; } Vector2 min = Camera.main.ViewportToWorldPoint(new Vector2(0, 0)); Vector2 max = Camera.main.ViewportToWorldPoint(new Vector2(1, 1)); Vector2 pos = transform.position; pos.x = Mathf.Clamp(pos.x, min.x + 0.5f, max.x); transform.position = pos; } else { rigidbody2D.velocity = new Vector2(0, rigidbody2D.velocity.y); anim.SetBool("Dash", false); } } void OnCollisionEnter2D(Collision2D col) { //Enemyとぶつかった時にコルーチンを実行 if (col.gameObject.tag == "Enemy") { StartCoroutine("Damage"); } } IEnumerator Damage() { //レイヤーをPlayerDamageに変更 gameObject.layer = LayerMask.NameToLayer("PlayerDamage"); //while文を10回ループ int count = 10; while (count > 0) { //透明にする renderer.material.color = new Color(1, 1, 1, 0); //0.05秒待つ yield return new WaitForSeconds(0.05f); //元に戻す renderer.material.color = new Color(1, 1, 1, 1); //0.05秒待つ yield return new WaitForSeconds(0.05f); count--; } //レイヤーをPlayerに戻す gameObject.layer = LayerMask.NameToLayer("Player"); }
}
Playerから出るミサイルのscript
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class PlayerMissile : MonoBehaviour
{
public LayerMask groundLayer; public GameObject Missile; public int shotCount; public Text shellLabel; private Rigidbody2D rigidbody2D; private Animator anim; private bool isGrounded; void Start() { anim = GetComponent<Animator>(); rigidbody2D = GetComponent<Rigidbody2D>(); shellLabel.text = "Missile:" + shotCount; } void Update() { if (Input.GetKeyDown("e")) { if (shotCount < 1) return; // ★追加 shotCountの数値を1ずつ減らす。 shotCount -= 1; // ★追加 shellLabel.text = "Missile:" + shotCount; anim.SetTrigger("Shot"); Instantiate(Missile, transform.position + new Vector3(0.4f, 1.2f, 0f), transform.rotation); // ショット音を鳴らす //GetComponent<AudioSource>().Play(); } } public void AddShell(int amount) { shotCount += amount; shellLabel.text = "Missile:" + shotCount; }
}
試したこと
武器の切り替えについて調べた。
回答2件