質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%

Q&A

解決済

2回答

1180閲覧

Unity 魔法弾などの発射

torn

総合スコア5

0グッド

1クリップ

投稿2020/12/18 03:18

Unity初心者で知識が乏しいため質問させていただきます。
敵から魔法弾のようなものを発射させて主人公に攻撃するようなプログラムを
書きたいのですが、どこを調べても出てこず、詰まっています。

現在カメラ内に敵が進入したら動くだけの動作になっています。
どのようにすれば魔法弾のような攻撃ができるでしょうか??

現在の敵のスクリプト

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.SceneManagement; 5 6public class mboss : MonoBehaviour 7{ 8 #region//インスペクターで設定する 9 [Header("移動速度")]public float speed; 10 [Header("重力")]public float gravity; 11 [Header("画面外でも行動する")] public bool nonVisibleAct; 12 [Header("接触判定")]public EnemyCollisionCheck checkCollision; 13 #endregion 14 15 #region//プライベート変数 16 private Rigidbody2D rb = null; 17 private SpriteRenderer sr = null; 18 private Animator anim = null; 19 private ObjectCollision oc = null; 20 private BoxCollider2D col = null; 21 private bool rightTleftF = false; 22 private bool isDead = false; 23 #endregion 24 25 // Start is called before the first frame update 26 void Start() 27 { 28 rb = GetComponent<Rigidbody2D>(); 29 sr = GetComponent<SpriteRenderer>(); 30 anim = GetComponent<Animator>(); 31 oc = GetComponent<ObjectCollision>(); 32 col = GetComponent<BoxCollider2D>(); 33 } 34 35 void FixedUpdate() 36 { 37 if (!oc.playerStepOn) 38 { 39 if (sr.isVisible || nonVisibleAct) 40 { 41 if(checkCollision.isOn) 42 { 43 rightTleftF = !rightTleftF; 44 } 45 46 int xVector = -1; 47 48 if (rightTleftF) 49 { 50 xVector = 1; 51 transform.localScale = new Vector3(-1, 1, 1); 52 } 53 else 54 { 55 transform.localScale = new Vector3(1, 1, 1); 56 } 57 rb.velocity = new Vector2(xVector * speed, -gravity); 58 } 59 else 60 { 61 rb.Sleep(); 62 } 63 } 64 else 65 { 66 if (!isDead) 67 { 68 SceneManager.LoadScene("mbossnazo"); 69 rb.velocity = new Vector2(0, -gravity); 70 isDead = true; 71 col.enabled = false; 72        Destroy(gameObject, 3f); 73 } 74 else 75 { 76 transform.Rotate(new Vector3(0, 0, 5)); 77 } 78 } 79 } 80}

Object Collisionのスクリプト

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class ObjectCollision : MonoBehaviour 6{ 7 [Header("これを踏んだ時のプレイヤーが跳ねる高さ")]public float boundHeight; 8 9 ///<summary> 10 ///このオブジェクトをプレイヤーが踏んだかどうか 11 ///</summary> 12 [HideInInspector]public bool playerStepOn; 13}

EnemyCollisionのスクリプト

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class EnemyCollisionCheck : MonoBehaviour 6{ 7 ///<summary> 8 ///判定内に敵か壁がある 9 ///</summary> 10 [HideInInspector]public bool isOn = false; 11 12 private string groundTag = "Ground"; 13 private string enemyTag = "Enemy"; 14 15 16 #region//接触判定 17 private void OnTriggerEnter2D(Collider2D collision) 18 { 19 if(collision.tag == groundTag || collision.tag == enemyTag) 20 { 21 isOn = true; 22 } 23 } 24 25 private void OnTriggerExit2D(Collider2D collision) 26 { 27 if(collision.tag == groundTag || collision.tag == enemyTag) 28 { 29 isOn = false; 30 } 31 } 32 #endregion 33} 34

GManagerのスクリプト

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class GManager : MonoBehaviour 6{ 7 public static GManager instance = null; 8 9 [Header("スコア")]public int score; 10 [Header("現在のスコア")]public int stageNum; 11 [Header("現在の復帰位置")]public int continueNum; 12 [Header("現在の残機")]public int heartNum; 13 [Header("デフォルトの残機")]public int defaultHeartNum; 14 [HideInInspector]public bool isGameOver = false; 15 16 private AudioSource audioSource = null; 17 18 private void Awake() 19 { 20 if(instance == null) 21 { 22 instance = this; 23 DontDestroyOnLoad(this.gameObject); 24 } 25 else 26 { 27 Destroy(this.gameObject); 28 } 29 } 30 31 private void Start() 32 { 33 audioSource = GetComponent<AudioSource>(); 34 } 35 36 ///<summary> 37 ///残機を1つ増やす 38 ///</summary> 39 public void AddHeartNum() 40 { 41 if(heartNum < 99) 42 { 43 ++heartNum; 44 } 45 } 46 47 ///<summary> 48 ///残機を1つ減らす 49 ///</summary> 50 public void SubHeartNum() 51 { 52 if(heartNum > 0) 53 { 54 --heartNum; 55 } 56 else 57 { 58 isGameOver = true; 59 } 60 } 61 62 ///<summary> 63 ///最初から始める時の処理 64 ///</summary> 65 public void RetryGame() 66 { 67 isGameOver = false; 68 heartNum = defaultHeartNum; 69 score = 0; 70 stageNum = 1; 71 continueNum = 0; 72 } 73 74 public void StartGame() 75 { 76 isGameOver = false; 77 heartNum = defaultHeartNum; 78 score = 0; 79 stageNum = 1; 80 continueNum = 0; 81 } 82 83 ///<summary> 84 ///SEを鳴らす 85 ///</summary> 86 public void PlaySE(AudioClip clip) 87 { 88 if(audioSource != null) 89 { 90 audioSource.PlayOneShot(clip); 91 } 92 else 93 { 94 Debug.Log("オーディオソースが設定されていません"); 95 } 96 } 97}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

sakura_hana

2020/12/18 07:34

まずはシューティングのチュートリアルが参考になるかと思いますのでやってみてください。 http://corevale.com/unity/6798 また、「魔法弾のようなもの」と言われてもわかりません。質問の際は具体的にどのような形状で、どのような条件でどこから発射され、どのように移動するかを記載してください。(下記も参照) https://teratail.com/help/question-tips
guest

回答2

0

ベストアンサー

PinoMatchさんと同じで
球を生成してからプレイヤーに向かって発射するので良いと思います。
また、この記事だったら敵や自キャラが向いている方向に球を打つなどができますのでぜひ参考にしてみてください。
Rigidbody.AddForce()などで飛ばせばいいと思いますよ。

投稿2020/12/19 02:19

negitam-31

総合スコア44

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

unity2dで弾を打つ。

それだけなら、さほど難しくはありません。
基本的な動作は

  1. 弾を生成する。
  2. プレイヤーの位置に向かって発射する。

これだけです。

この記事とか参考になるんじゃないかな、と思います。

投稿2020/12/18 09:43

PinoMatcha

総合スコア368

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問