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

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

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

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

0回答

742閲覧

Unityの当たり判定

0mizunomitai

総合スコア1

Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2020/10/05 21:17

前提・実現したいこと

2Dのアクションゲームを作っていて、敵レイヤーにPlayerのアタックポイントを攻撃で当てて"IsHurt"アニメーション
を実行したいです。

レイヤーに当たり判定という意味もよくわかっていません。

該当のソースコード

Unity

1//EnemyManager// 2 3using System.Collections; 4using System.Collections.Generic; 5using UnityEngine; 6 7public class EnemyManager : MonoBehaviour 8{ 9 Animator animator; 10 // Start is called before the first frame update 11 void Start() 12 { 13 animator = GetComponent<Animator>(); 14 } 15 16 // Update is called once per frame 17 public void OnDamage() 18 { 19 animator.SetTrigger("IsHurt"); 20 } 21} 22 23 24 25 26//PlayerManager// 27 28 29using System.Collections; 30using System.Collections.Generic; 31using UnityEngine; 32 33public class PlayerManager : MonoBehaviour 34{ 35 public float moveSpeed = 3; 36 Rigidbody2D rb; // Start is called before the first frame update 37 Animator animator; 38 public Transform attackPoint; 39 public float attackRadius; 40 public LayerMask enemyLayer; 41 void Start() 42 { 43 rb = GetComponent<Rigidbody2D>(); 44 animator = GetComponent<Animator>(); 45 } 46 47 // Update is called once per frame 48 void Update() 49 { 50 if (Input.GetKeyDown(KeyCode.Space)) 51 { 52 Attack(); 53 } 54 Movement(); 55 } 56 void Attack(){ 57 animator.SetTrigger("IsAttack"); 58 Collider2D[] hitEnemys = Physics2D.OverlapCircleAll(attackPoint.position, attackRadius, enemyLayer); 59 foreach (Collider2D hitEnemy in hitEnemys) 60 { 61 hitEnemy.GetComponent<EnemyManager>().OnDamage(); 62 } 63 } 64 private void OnDrawGizmosSelected() 65 { 66 Gizmos.color = Color.red; 67 Gizmos.DrawWireSphere(attackPoint.position, attackRadius); 68 } 69 void Movement() 70 { 71 float x = Input.GetAxisRaw("Horizontal"); 72 if (x > 0) 73 { 74 transform.localScale = new Vector3(-1, 1, 1); 75 } 76 77 if (x < 0) 78 { 79 transform.localScale = new Vector3(1, 1, 1); 80 } 81 animator.SetFloat("Speed", Mathf.Abs(x)); 82 rb.velocity = new Vector2(x * moveSpeed, rb.velocity.y); 83 } 84} 85

試したこと

Enemyレイヤーの作成、アタッチ。AttakPointの作成、設定。
Enemy・PlayerともにBoxcollider2D・Rigidbody2Dがついています。

Unity始めたばかりでまだ理解できていません。
情報が足りないかもしれませんが、足りない部分や不明な点がありましたらよろしくお願いいたします。

補足情報(FW/ツールのバージョンなど)

Unity 2019.4.11f1 です。

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

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

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

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

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

sakura_hana

2020/10/06 01:20

衝突を取得するメソッド(OnCollisionEnter2D等)が無いので当然動作しません。 また、「レイヤー」の認識がおかしいように思います。 解説サイトや本を見ていちから学んだ方がいいと思います(既に見ているならその説明が悪いので違う資料を見た方がいいです)。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問