自分がプレイする物体を自由に動かせるようになりたい。
プレイする為のオブジェクトを入れそれに何かコンポーネントを入れスプリクトを作ってどうとか…
検索すればある程度のことは出てくるということで調べてやってみました。
UNITYを開き
3Dでプロジェクトを作成
導入したGameObject
Plane(平面)、Sphere(スフィア)、Cube(キューブ)
※Planeは床、Sphereを動かしたいプレイヤー、Cubeは障害物として設置しました。
オブジェクトに入れたコンポーネント
Plane、Cubeは既存のものをそのままに
Spehereには[Character Controller]、[Rigidbody]、[C#Script]を導入
移動、ジャンプの仕方を検索して組み合わせた結果
SpehereのC#スクリプトの内容は下記のようになりました。
using System.Collections;using System.Collections.Generic;using UnityEngine;public class Player : MonoBehaviour{public float speed = 5.0f;float moveX = 0f;float moveZ = 0f;private CharacterController controller;private Vector3 moveDirection;void Start(){// コンポーネントCharacterControllerの取得controller = GetComponent<CharacterController>();}void Update(){// オブジェクトの移動moveX = Input.GetAxis("Horizontal") * speed;moveZ = Input.GetAxis("Vertical") * speed;Vector3 direction = new Vector3(moveX, 0, moveZ);controller.SimpleMove(direction);// もし着地していたらif (controller.isGrounded){// スペースキーの入力が入るとジャンプif (Input.GetKeyDown(KeyCode.Space)){// ジャンプ力moveDirection.y = 10;}}// オブジェクトの重力moveDirection.y -= 10 * Time.deltaTime;// オブジェクトを動かす処理controller.Move(moveDirection * Time.deltaTime);}}
プレイしてみたらこんな感じになりました。