カロリーメイトください

Barbaroi Ware(バルバロイ・ウェア)という名前でアプリ開発してます

『Unity5の教科書』「Chapter6 Physicsとアニメーション」 学習記録

序文

「Unity5の教科書」学習6日目。

今週は(も)生活リズムが崩れていてつらいです。

GitHub

github.com

進捗

  • 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");
  }
}
  • その他実装部分

github.com

実行結果

www.youtube.com

感想

今日の課題は猫を操作してどんどん雲の上をのぼっていくスマホ向けゲーム。

アニメーションのさせ方なんかはなかなかUnity独特の操作でおもしろい。
Physicsを使った衝突判定なんて内容も出てきて、ここまでの内容だけでもずいぶんゲームらしいゲームが作れるんじゃないかなと思う。
ファミコンレベルのゲームなら結構作れるんじゃないかなー。

実際はグラフィックスを準備するのが一番大変なんだけどね!

カロリーメイトください。

BGM

Countdown / chelmico

www.youtube.com