【Unity8】ユニティちゃんが攻撃!SetTrigger, Instantiate【ユニティちゃん横スクロール5】
Unity5.1.0f3 Personal(2015年6月)
このコンテンツは、『ユニティちゃんライセンス』で提供されています
【目標】ユニティちゃんに攻撃させる
①Bulletの作成
まずは攻撃した時に出す弾を作りましょう〜以下の画像をSpritesフォルダにImportして下さい〜
bullet.png
Inspectorの設定は以下の通りです〜
Sprite Editorの設定は以下の通りです〜
2枚のSprite画像が完成したら、Hierarchyビューへドラッグ&ドロップし、更にPrefabsフォルダへ移してPrefab化します〜Prefab化が完了したら、Hierarchyビューのbulletは削除して下さい〜
Prefab化し、元のオブジェクトは削除 Prefabの名前は「Bullet」に変更
BulletにはRigidbody2Dコンポーネントを付け、
Prefab化できたら、BulletScriptを作成したプレファブBulletに取り付けます〜以下ソースです〜
using UnityEngine; using System.Collections; public class BulletScript: MonoBehaviour { private GameObject player; private int speed = 10; void Start () { //ユニティちゃんオブジェクトを取得 player = GameObject.FindWithTag("UnityChan"); //rigidbody2Dコンポーネントを取得 Rigidbody2D rigidbody2D = GetComponent<Rigidbody2D>(); //ユニティちゃんの向いている向きに弾を飛ばす rigidbody2D.velocity = new Vector2 (speed * player.transform.localScale.x, rigidbody2D.velocity.y); //画像の向きをユニティちゃんに合わせる Vector2 temp = transform.localScale; temp.x = player.transform.localScale.x; transform.localScale = temp; //5秒後に消滅 Destroy(gameObject, 5); } }
BulletScript.cs
まずはユニティちゃんに「UnityChan」という名前のタグを付けて下さい〜
File > Project Settings > Tags & Layers からタグの追加
タグを付ける
※※※2015年11月14日追記※※※
このままだとBulletが重力の影響を受けてしまうので、
BulletについているRigidbody2DのIs Kinematicにチェックを入れるか、
Gravity Scaleを0にするかして下さい!
コメントありがとうございました〜!
※※※※※※※※※※※※※※※※※※※※※※
スクリプトの流れとしては、
①ユニティちゃんのオブジェクト情報を取得
②ユニティちゃんの向いている方向に弾を飛ばす
ユニティちゃんが右を向いている時はlocalScale.xが1に、左を向いている時はlocalScale.xが-1になっているのを利用しています〜
③ユニティちゃんが向いている方向に弾の向きを変える
④5秒後に弾を消滅(Destroy)させる
という風になっています〜
②弾を撃つアニメーションの作成
それではユニティちゃんに、新たに弾を撃つアニメーションを追加していきましょう〜
HierarchyビューでUnityChanを選択した状態でAnimationビューを開き、Create New Clipを実行して下さい〜アニメーションの名前は「Shot@Player」です〜
Create New ClipでShot@Playerアニメーションを作成する
続いてAdd Property > Sprite Renderer > Spriteを選択し、Samplesを16、アニメーション位置0.0にユニティちゃん_待機_SHOT_0、0.1の位置にユニティちゃん_待機_SHOT_1を配置して下さい〜
SHOTアニメーションを配置
続いてアニメーターの設定をしていきます〜Animatorビューを開くとShot@Playerのアニメーションが追加されているので、Wait@Playerの右に配置し、Make Transitionにて相互に矢印で結びます〜
続いてアニメーター内にTrigger型の変数Shotを作成します〜
次にWait@PlayerからShot@Playerへ向いている矢印をクリックし、Inspectorビューの設定をしていきます〜
Trigger変数Shotがtrueになった時にShot@Playerを再生したいので、Has Exit Timeはfalse、Transition Durationは0、ConditionsにTrigger変数Shotを設定します〜
Shot@PlayerからWait@Playerへ向いている矢印のInspectorも設定します〜こちらは、1回実行して戻ればいいのでHas Exit Timeはtrue、Exit Timeは1、Transition Durationは0にします〜
③実際に弾を撃たせてみる
それではPlayerScriptに追記していき、実際に弾を撃たせてみましょう〜以下ソースです〜
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; void Start () { anim = GetComponent<Animator>(); rigidbody2D = GetComponent<Rigidbody2D>(); } void Update () { isGrounded = Physics2D.Linecast ( transform.position + transform.up * 1, transform.position - transform.up * 0.05f, groundLayer); if (Input.GetKeyDown ("space")) { if (isGrounded) { 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 ("left ctrl")){ anim.SetTrigger("Shot"); Instantiate(bullet, transform.position + new Vector3(0f,1.2f,0f), transform.rotation); } //********** 終了 **********// } 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); } } }
PlayerScript.cs
まずはpublic変数のbulletにPrefabのBulletを設定してあげましょう〜
ドラッグ&ドロップ!
左のcontrolキーを押した時にアニメーターのTrigger変数Shotをtrueにし、InstantiateにてBulletを生成しています〜生成位置はユニティちゃんの足元からY軸方向に1.2上に調整しています〜
【結果】
いい感じに弾を撃てるようになりました〜
今回はここまでです〜
ありがとうございました〜
【Unity開発8】ユニティちゃんを表示させる【横スクロールユニティちゃん1】
【Unity開発8】ユニティちゃんを歩かせる【横スクロールユニティちゃん2】
【Unity開発8】カメラにユニティちゃんを追いかけさせる【横スクロールユニティちゃん3】
【Unity開発8】ユニティちゃんをジャンプさせる【横スクロールユニティちゃん4】
【Unity開発8】ユニティちゃんバスターで攻撃する【ユニティちゃん横スクロール5】
【Unity開発8】走っている時やジャンプ中にも弾を撃つ【横スクロールユニティちゃん6】
【Unity開発8】弾を当てて敵を倒す【横スクロールユニティちゃん7】
【Unity開発8】uGUIでライフを作り、ダメージを実装する【横スクロールユニティちゃん8】
【Unity開発8】ダメージを食らった時、一定時間無敵状態にする【横スクロールユニティちゃん9】
【Unity開発8】アイテムを取得して体力回復する【横スクロールユニティちゃん10】
【Unity開発8】敵キャラを倒した時にアイテムを落とさせる【横スクロールユニティちゃん11】
【Unity開発8】Main Cameraに映るまで敵キャラを待機させておく【横スクロールユニティちゃん12】