#2 Playerを動かして弾を発射できるようにしよう【Unity】【チュートリアル】
動かすためにスクリプトを書いていこう
動かすためにスクリプトを書いていく前に,スクリプトを保存するフォルダーを作ろう.
Projectビューから,Create→Folderをクリックして名前はScriptsに設定しよう.
それができれば,Scriptsフォルダの中で,Projectビューから,Create→C# Scriptをクリックして名前はPlayerで作成しよう!!
作成できたら作成したPlayerスクリプトをダブルクリックして,以下の内容を貼り付けよう!
using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public class Player : MonoBehaviour { void Update() { // Player 移動 if(Input.GetKey(KeyCode.DownArrow)) { // z軸方向(後ろ)に移動 this.transform.Translate (0.0f, 0.0f, -10.0f * Time.deltaTime); } else if(Input.GetKey(KeyCode.UpArrow)) { // z軸方向(前)に移動 this.transform.Translate (0.0f, 0.0f, 10.0f * Time.deltaTime); } if(Input.GetKey(KeyCode.LeftArrow)) { // y軸左方向に回転 this.transform.Rotate(0.0f,-50.0f* Time.deltaTime,0.0f); } else if(Input.GetKey(KeyCode.RightArrow)) { // y軸右方向に回転 this.transform.Rotate(0.0f,50.0f* Time.deltaTime,0.0f); } } }
*{}の書き方はC#の書き方に従っているのでC++とかやってた人はすこし違和感があるかもしれません.僕もあります.
貼り付けて保存できたら,一度Unity上で実行してみよう
実行できたら,矢印キーを使ってPlayerを動かせるでしょうか?
動かせていたら成功です.自由に白い床の上を動かしてみましょう
もし動かせない場合は,一度Gameビューをクリックしてから矢印キーを押してみましょう.
Playerから玉を飛ばせるようにしよう
HierarchyビューからCreate → Create Emptyをクリックして名前をmuzzleに設定.
muzzleをPlayerにドラッグしてPlayerの中に入っていることを確認する.
muzzleの位置は以下のように設定する. muzlleは発射位置なのでPlayerと位置がかぶっていたらあまり良くない.
次は,飛ばす為の弾を作ろう.
HierarchyビューからCreate → 3D object → Sphere をクリックし名前はbulletに設定.
Scaleは,すこし小さめのx,y,zすべて0.5に設定しよう.
bulletは飛ばしたいので重力などいろいろ計算してくれるRigidbodyを付ける必要がある.
bulletのInspectorから,一番下のAdd Componentを押し検索窓からRigidbodyを検索,もしくはPhysics→Rigidbodyを選択する.
色も白色なので#1で作成したようにbulletという名前のMaterialを作成し黒い色にする.
そして,設定できたら,Prefab化をしておきたいのでHierarchyビューからbulletのオブジェクトをProjectビューにドラッグする.Prefab化ができたらHierarchyビューにあるbulletは削除しておく.
設定できたら,Playerスクリプトを以下のように変更.
using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public class Player : MonoBehaviour { // 弾丸 [SerializeField] private GameObject bullet; // 銃口 [SerializeField] private GameObject muzzle; // 発射する強さ [SerializeField] private float speed = 1000; // Start is called before the first frame update void Start() { // インスペクターで設定されてなければ探し取り込む if(!bullet){ bullet = GameObject.Find("bullet"); } if(!muzzle){ muzzle = GameObject.Find("Muzzle"); } } // Update is called once per frame void Update() { // Player 移動 if(Input.GetKey(KeyCode.DownArrow)){ this.transform.Translate (0.0f, 0.0f, -10.0f * Time.deltaTime); } else if(Input.GetKey(KeyCode.UpArrow)){ this.transform.Translate (0.0f, 0.0f, 10.0f * Time.deltaTime); } if(Input.GetKey(KeyCode.LeftArrow)){ this.transform.Rotate(0.0f,-50.0f* Time.deltaTime,0.0f); } else if(Input.GetKey(KeyCode.RightArrow)){ this.transform.Rotate(0.0f,50.0f* Time.deltaTime,0.0f); } // 弾丸の発射 if (Input.GetKeyDown(KeyCode.Space)){ // 弾丸の複製 GameObject bullets = Instantiate(bullet); Vector3 force; force = this.gameObject.transform.forward * speed; // Rigidbodyに力を加えて発射 bullets.GetComponent<Rigidbody>().AddForce(force); // 弾丸の位置を調整 bullets.transform.position = muzzle.transform.position; } } }
変更するとUnityの方でPlayerオブジェクトのInspectorビューに以下のような箇所が追加されている.
bulletは先程prefab化した,bullet prefabをドラッグ,
muzzleは,Playerオブジェクトの下にあるmuzzleをドラッグする.
speedは,弾を発射する時の速さである.
設定したら実行してみよう.
[:e300]
弾が消えない問題をなんとかしよう
実行して玉を打った人は気づくと思う,bulletめっちゃ生成されてることに.
bulletにも床から落ちたら消滅するスクリプトいるということに...じゃあ停止してスクリプト作りましょう.
ProjectウィンドウのScriptsフォルダでCreate → C# Scriptをクリックしてbulletという名前のファイルを作成しよう.
作成したファイルをbulletのprefabに貼り付けよう.
貼り付け方は,ProjectウィンドウのPrefabフォルダのbulletをダブルクリックもしくは,InspectorビューのOpen Prefabをクリック.
いつもと同じように,Scriptsフォルダのbullet.csをドラッグするとつけられる.
Hierarchyビューがbulletだけになるが一番上の"<"のようなボタンを押すことで戻れる.
スクリプトbulletを以下のように編集しよう.
using System.Collections; using System.Collections.Generic; using UnityEngine; public class bullet : MonoBehaviour { void Update() { // bullet が床から落ちれば消滅させる if(this.transform.position.y < -1) { Destroy(this.gameObject); } } }
スクリプトが保存できたら,床のギリギリまで行って玉を打ってみよう.
落ちた弾はすぐ消えることが確認できると思います.