2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Unity】Character Controllerでキャラクター移動とJumpをする(Mac bookのキーボードを使う)

Posted at

環境メモ
⭐️Mac OS Mojave バージョン10.14
⭐️Unity 2018.2.15f1
⭐️Mac book

Character Controllerでキャラクター移動(矢印キー)とJump(Spaceキー押下)をする (Mac bookのキーボードを使う)

完成イメージ
RedHair.gif

スクリーンショット 2019-01-05 17.03.44.png

1.キャラクターに、Character Controllerを設定する
「Component」-「Physics」-「Character Controller」を選択する
スクリーンショット 2019-01-05 16.35.00.png

2.コライダーを設定する。移動スクリプトをコンポーネント追加する。
スクリーンショット 2019-01-05 16.56.53.png

RedHairScript.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RedHairScript : MonoBehaviour {
    CharacterController characterController;

    public float speed = 6.0f;
    public float jumpSpeed = 8.0f;
    public float gravity = 20.0f;

    private Vector3 moveDirection = Vector3.zero;

    void Start () {
        characterController = GetComponent<CharacterController>();
    }

	void Update () {
        if (characterController.isGrounded)
        {
            moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical"));
            moveDirection *= speed;

            if (Input.GetKey(KeyCode.Space))
            {
                moveDirection.y = jumpSpeed;
            }
          
        }
        moveDirection.y -= gravity * Time.deltaTime;
        characterController.Move(moveDirection * Time.deltaTime);
    }
}
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?