カロリーメイトください

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

『Unity5ゲーム開発レシピ』「Part01 基本編 Chapter02 プレイヤーを動かす」 学習記録

Unity5ゲーム開発レシピ ハマるゲーム制作のノウハウ

Unity5ゲーム開発レシピ ハマるゲーム制作のノウハウ

序文

『Unity5ゲーム開発レシピ』2日目。

サンプルを解凍しようと思ったら「未対応の形式です」とか言われて、その対応に大半の時間が割かれてしまった。
サンプルファイルデカいよ!!

GitHub

github.com

進捗

  • Part01 基本編
    • Chapter02 プレイヤーを動かす(途中)

(学習時間:1時間)

コード実装部分

\Chapter1\Assets\Scripts\PlayerMove.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMove : MonoBehaviour
{
  public float speed = 15.0f;
  public float jumpSpeed = 8.0f;
  public float gravity = 20.0f;
  private Vector3 moveDirection = Vector3.zero;

  void Start()
  {

  }

  void Update()
  {
    // プレイヤーを移動させる
    CharacterController controller = GetComponent<CharacterController>();

    if (controller.isGrounded)
    {
      // 動く方向を取得するGetAxis()は-1~+1の値を返す
      moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
      // ローカル座標をワールド座標に変換する
      moveDirection = transform.TransformDirection(moveDirection);
      moveDirection *= speed;

      // ジャンプ処理
      if (Input.GetButton("Jump"))
        moveDirection.y = jumpSpeed;
    }

    // 重力を考慮する
    moveDirection.y -= gravity * Time.deltaTime;

    // 動かす
    controller.Move(moveDirection * Time.deltaTime);
  }
}

実行結果

www.youtube.com しゅーる

感想

CharacterControllerなる便利クラスの存在を知った。
2Dでも使えるのかな?

そうそう、こういう王道処理のやり方みたいなのが知りたいんだよ。
あんまり読者に親切な感じは見受けられないけど、しっかりやれば結構役に立つかもしれない。

でもこういうのももっと便利な拡張アセットみたいなのがあったりするのかなー。

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

BGM

X-amount / KICK THE CAN CREW

www.youtube.com