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

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

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

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

Q&A

解決済

1回答

13578閲覧

unityのtagとlayerの使い分けについて

ayousanz

総合スコア258

Unity

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

0グッド

1クリップ

投稿2019/02/22 05:40

unityでキャラクターをジャンプをする時に地面と判断をしたいと思っています.
その中で"tag"と"layer"があるのですが,いまいち使い分けがわかりません.
どのように使い分ければいいのでしょうか?
宜しくお願いします.
以下が参考にさせていただいたコード・サイトです.
Unity2D ~キャラクター操作ジャンプ編~

c#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class Player : MonoBehaviour { 6 7 //変数定義 8 public float flap = 1000f; 9 public float scroll = 5f; 10 float direction = 0f; 11 Rigidbody2D rb2d; 12 bool jump = false; 13 14 // Use this for initialization 15 void Start () { 16 //コンポーネント読み込み 17 rb2d = GetComponent<Rigidbody2D>(); 18 } 19 20 21 // Update is called once per frame 22 void Update () { 23 24 //キーボード操作 25 if (Input.GetKey(KeyCode.RightArrow)) 26 { 27 direction = 1f; 28 }else if (Input.GetKey(KeyCode.LeftArrow)) 29 { 30 direction = -1f; 31 }else 32 { 33 direction = 0f; 34 } 35 36 37 //キャラのy軸のdirection方向にscrollの力をかける 38 rb2d.velocity = new Vector2(scroll * direction, rb2d.velocity.y); 39 40 //ジャンプ判定 41 if (Input.GetKeyDown("space") && !jump) 42 { 43 rb2d.AddForce(Vector2.up * flap); 44 jump = true; 45 } 46 47 48 } 49 50 void OnCollisionEnter2D(Collision2D other) 51 { 52 if (other.gameObject.CompareTag("Ground")) 53 { 54 jump = false; 55 } 56 } 57}

【Unity8】ユニティちゃんがジャンプ!Input.GetKeyDown, AddForce【横スクロールユニティちゃん4】

c#

1using UnityEngine; 2using System.Collections; 3 4public class PlayerScript : MonoBehaviour { 5 6 public float speed = 4f; 7//********** 開始 **********// 8 public float jumpPower = 700; //ジャンプ力 9 public LayerMask groundLayer; //Linecastで判定するLayer 10//********** 終了 **********// 11 public GameObject mainCamera; 12 private Rigidbody2D rigidbody2D; 13 private Animator anim; 14//********** 開始 **********// 15 private bool isGrounded; //着地判定 16//********** 終了 **********// 17 18 void Start () { 19 anim = GetComponent<Animator>(); 20 rigidbody2D = GetComponent<Rigidbody2D>(); 21 } 22//********** 開始 **********// 23 void Update () 24 { 25 //Linecastでユニティちゃんの足元に地面があるか判定 26 isGrounded = Physics2D.Linecast ( 27 transform.position + transform.up * 1, 28 transform.position - transform.up * 0.05f, 29 groundLayer); 30 //スペースキーを押し、 31 if (Input.GetKeyDown ("space")) { 32 //着地していた時、 33 if (isGrounded) { 34 //Dashアニメーションを止めて、Jumpアニメーションを実行 35 anim.SetBool("Dash", false); 36 anim.SetTrigger("Jump"); 37 //着地判定をfalse 38 isGrounded = false; 39 //AddForceにて上方向へ力を加える 40 rigidbody2D.AddForce (Vector2.up * jumpPower); 41 } 42 } 43 //上下への移動速度を取得 44 float velY = rigidbody2D.velocity.y; 45 //移動速度が0.1より大きければ上昇 46 bool isJumping = velY > 0.1f ? true:false; 47 //移動速度が-0.1より小さければ下降 48 bool isFalling = velY < -0.1f ? true:false; 49 //結果をアニメータービューの変数へ反映する 50 anim.SetBool("isJumping",isJumping); 51 anim.SetBool("isFalling",isFalling); 52 } 53//********** 終了 **********// 54 55 void FixedUpdate () 56 { 57 float x = Input.GetAxisRaw ("Horizontal"); 58 if (x != 0) { 59 rigidbody2D.velocity = new Vector2 (x * speed, rigidbody2D.velocity.y); 60 Vector2 temp = transform.localScale; 61 temp.x = x; 62 transform.localScale = temp; 63 anim.SetBool ("Dash", true); 64 if (transform.position.x > mainCamera.transform.position.x - 4) { 65 Vector3 cameraPos = mainCamera.transform.position; 66 cameraPos.x = transform.position.x + 4; 67 mainCamera.transform.position = cameraPos; 68 } 69 Vector2 min = Camera.main.ViewportToWorldPoint(new Vector2(0, 0)); 70 Vector2 max = Camera.main.ViewportToWorldPoint(new Vector2(1, 1)); 71 Vector2 pos = transform.position; 72 pos.x = Mathf.Clamp(pos.x, min.x + 0.5f, max.x); 73 transform.position = pos; 74 } else { 75 rigidbody2D.velocity = new Vector2 (0, rigidbody2D.velocity.y); 76 anim.SetBool ("Dash", false); 77 } 78 } 79}

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

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

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

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

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

guest

回答1

0

ベストアンサー

Layer
特定のオブジェクトを無視したいときに使います
・カメラに映らないオブジェクトを作るときはLayerで指定します
・RayCastという、ぶつかるオブジェクトを探すときに
特定のオブジェクトを無視したいときにLayerで指定します
・32個しか作れませんし、一部はシステムで予約されています

Tag
オブジェクトをまとめて処理したい時に向いています
・FindGameObjectWithTagを使うことでシーンにあるタグがついたオブジェクトを一括して取得することが出来ます
・FindGameObjectWithTagはFind系列の中では最速のようです
参考:http://baba-s.hatenablog.com/entry/2014/07/09/093240

オブジェクトの属性という意味ではどちらも使えますが、
Layerは個数制限があるので、どっちでもいいならTagを使うほうがいいと思います

投稿2019/02/22 09:44

izmktr

総合スコア2856

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

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

ayousanz

2019/02/22 09:51

ありがとうございます!! 今はtagの方を使っていきたいと思います.
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問