
Unityの教科書 Unity 2017完全対応版 2D&3Dスマートフォンゲーム入門講座 (Entertainment&IDEA)
- 作者: 北村愛実
- 出版社/メーカー: SBクリエイティブ
- 発売日: 2017/09/21
- メディア: 単行本
- この商品を含むブログを見る
序文
「Unity5の教科書」学習6日目。
今週は(も)生活リズムが崩れていてつらいです。
GitHub
進捗
- Chapter6 Physicsとアニメーション
(学習時間:3時間)
コード実装部分(一部)
\ClimbCloud\Assets\PlayerController.cs
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; public class PlayerController : MonoBehaviour { Rigidbody2D rigid2D; Animator animator; float jumpForce = 680.0f; float walkForce = 30.0f; float maxWalkSpeed = 2.0f; void Start() { this.rigid2D = GetComponent<Rigidbody2D>(); this.animator = GetComponent<Animator>(); } void Update() { // 画面外に出た場合は最初から if(transform.position.y < -10) { SceneManager.LoadScene("GameScene"); } // ジャンプする if (Input.GetKeyDown(KeyCode.Space) && this.rigid2D.velocity.y == 0) { this.animator.SetTrigger("JumpTrigger"); this.rigid2D.AddForce(transform.up * this.jumpForce); } // 左右移動 int key = 0; if (Input.GetKey(KeyCode.RightArrow)) key = 1; if (Input.GetKey(KeyCode.LeftArrow)) key = -1; // プレイヤの速度 float speedx = Mathf.Abs(this.rigid2D.velocity.x); // スピード制限 if (speedx < this.maxWalkSpeed) { this.rigid2D.AddForce(transform.right * key * this.walkForce); } // 動く方向に応じて反転 if (key != 0) { transform.localScale = new Vector3(key, 1, 1); } // プレイヤの速度に応じてアニメーション速度を変える if (this.rigid2D.velocity.y == 0) { this.animator.speed = speedx / 2.0f; } else { this.animator.speed = 1.0f; } } // ゴールに到達 private void OnTriggerEnter2D(Collider2D other) { // Debug.Log("ゴール"); SceneManager.LoadScene("ClearScene"); } }
- その他実装部分
実行結果
感想
今日の課題は猫を操作してどんどん雲の上をのぼっていくスマホ向けゲーム。
アニメーションのさせ方なんかはなかなかUnity独特の操作でおもしろい。
Physicsを使った衝突判定なんて内容も出てきて、ここまでの内容だけでもずいぶんゲームらしいゲームが作れるんじゃないかなと思う。
ファミコンレベルのゲームなら結構作れるんじゃないかなー。
実際はグラフィックスを準備するのが一番大変なんだけどね!
カロリーメイトください。
BGM
Countdown / chelmico