Unityのゲーム作成の説明を書いていきます!
5月24日(土)
ゲームを作る
まず3Dでプレイヤーを動かしてみた
コード
using UnityEngine;
public class Player : MonoBehaviour
{
[SerializeField] private float moveSpeed; // 移動速度
[SerializeField] private float jumpForce; // ジャンプ力
private Rigidbody rb; // プレイヤーのRigidbody
private bool isGrounded; // 地面に接しているかどうか
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
Move();
Jump();
}
void Move()
{
Vector3 moveDirection = Vector3.zero;
if (Input.GetKey(KeyCode.UpArrow))
{
moveDirection += transform.forward;
}
if (Input.GetKey(KeyCode.DownArrow))
{
moveDirection -= transform.forward;
}
if (Input.GetKey(KeyCode.LeftArrow))
{
moveDirection -= transform.right;
}
if (Input.GetKey(KeyCode.RightArrow))
{
moveDirection += transform.right;
}
Vector3 move = moveDirection.normalized * moveSpeed;
Vector3 velocity = rb.linearVelocity;
rb.linearVelocity = new Vector3(move.x, velocity.y, move.z);
}
void Jump()
{
if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
{
rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
isGrounded = false;
}
}
void OnCollisionEnter(Collision collision)
{
// 地面に着いたらジャンプ可能にする
if (collision.gameObject.CompareTag("Ground"))
{
isGrounded = true;
}
}
}
地面にGroundタグを設定
前後左右ジャンプができるようになった!
プレイヤー(立方体)の子にカメラを入れて、位置を調整
プレイヤーについてくるカメラの完成!
新しくCubeを作成
CubeにRigidbodyをつけ、Constraintsの中のFreezeRotationのx,y,z全てにチェックを入れます
こうすると回転しなくなります
MASSを14にしてください

5月27日(火)
壁を作る

Wallというファイルを作り、手前には、Colliderを、奥と右と、左は壁を追加
ドアを配置する


親にGameObjectを、子にCubeを追加します。親の点が右下に来るように、Cubeの位置を調整してください
Cubeのサイズはx,2.5,y,4.5,z,0.1
左下にすることで、Doorのオブジェクトを回転した時にそこが中心になって回る
下のように調整します

5月28日(水)
スイッチを作る
using System;
using System.Linq;
using UnityEngine;
public class Swich : MonoBehaviour
{
[SerializeField] public DoorOpen[] doors;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
}
// Update is called once per frame
void Update()
{
}
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Player") || collision.gameObject.CompareTag("Block"))
{
foreach (var door in doors)
{
door.Open();
}
}
}
}
Swichを作り、BoxColliderとRigidbodyをつける
SwitchコンポーネントのDoorsの中にドアのオブジェクトを入れる
ドアプログラム
using UnityEngine;
public class DoorOpen : MonoBehaviour
{
private Animator animation;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
animation = gameObject.GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
}
public void Open()
{
animation.SetTrigger("Open");
}
}
Animationを追加

Doorのところにはアニメーションを入れる
プレイヤーをコントローラーでも動かせるようにする
using UnityEngine;
public class Player : MonoBehaviour
{
[SerializeField] private float moveSpeed; // 移動速度
[SerializeField] private float jumpForce; // ジャンプ力
[SerializeField] private float deadZone = 0.1f; // コントローラーのデッドゾーン
private Rigidbody rb; // プレイヤーのRigidbody
private bool isGrounded; // 地面に接しているかどうか
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
Move();
Jump();
}
void Move()
{
Vector3 moveDirection = Vector3.zero;
// キーボード入力
if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.W))
{
moveDirection += transform.forward;
}
if (Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.S))
{
moveDirection -= transform.forward;
}
if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A))
{
moveDirection -= transform.right;
}
if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D))
{
moveDirection += transform.right;
}
// コントローラー入力(左スティック)
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
// デッドゾーン処理
if (Mathf.Abs(horizontal) > deadZone)
{
moveDirection += transform.right * horizontal;
}
if (Mathf.Abs(vertical) > deadZone)
{
moveDirection += transform.forward * vertical;
}
Vector3 move = moveDirection.normalized * moveSpeed;
Vector3 velocity = rb.linearVelocity;
rb.linearVelocity = new Vector3(move.x, velocity.y, move.z);
}
void Jump()
{
// キーボード(スペースキー)またはコントローラー(×ボタン)でジャンプ
if ((Input.GetKeyDown(KeyCode.Space) || Input.GetKeyDown(KeyCode.Joystick1Button1)) && isGrounded)
{
rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
isGrounded = false;
}
}
void OnCollisionEnter(Collision collision)
{
// 地面に着いたらジャンプ可能にする
if (collision.gameObject.CompareTag("Ground") || collision.gameObject.CompareTag("Block"))
{
isGrounded = true;
}
}
}
// キーボード(スペースキー) またはコントローラー(×ボタン) でジャンプの2つ下の
Joystick1Button1が違う人もいるかもしれません。最後の数字を0~3で変えて試してみてください