2023プログラミング:Unityのプログラミング2

ゲームステージの拡張
ゲームのステージに動くオブジェクトを追加して拡張していきます。

動く道
動く道を追加します。ヒエラルキーウィンドウのRoadを複製し、インスペクターウィンドウでMoveRoadという名称に変更します。
MoveRoadにRigidbodyコンポーネントをアタッチします。RigidbodyのUse Gravityのチェックマークを外し、Is Kinematicにチェックマークをつけます。
ConstraintsのFreeze RotationのX,Y,Zにチェックマークを付けます。

ScriptsフォルダにMoveRoadという名前のスクリプトを作成します。MoveRoadスクリプトをゲームオブジェクトMoveRoadにドラッグ・アンド・ドロップします。MoveRoadスクリプトがゲームオブジェクトMoveRoadにアタッチできているかをインスペクターウィンドウで確認します。

MoveRoadスクリプトに以下のコードを記述します。

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

public class MoveRoad : MonoBehaviour
{
    private Rigidbody roadRigid;
    private Vector3 defPos;

    void Start()
    {
        roadRigid = GetComponent<Rigidbody>();
        defPos = transform.position;
    }

    void Update()
    {
        
    }

    void FixedUpdate()
    {
        roadRigid.MovePosition(
            new Vector3(defPos.x , defPos.y, defPos.z + Mathf.PingPong(Time.time, 4)));
    }
}

スクリプトの動作確認をします。

MovePositionメソッドを使用して、ゲームオブジェクトの座標を移動させています。

Rigidbody型の変数.MovePosition(移動先の座標)

Mathf.PingPomgメソッドを使用して往復する動きを作ります。

Mathf.PingPong(時間,数値)

Mathf.PingPong(Time.time, 4)の記述を、それぞれ別の座標に足すことで、移動方向を変更できます。
また数値を変更することで、移動距離を変更できます。

例)上下方向に倍の距離を移動する道を作成する。

roadRigid.MovePosition(
            new Vector3(defPos.x , defPos.y + Mathf.PingPong(Time.time, 8), defPos.z ));

 

障害物の作成
キャラクターを突き落とす障害物を作成します。ヒエラルキーウィンドウに新たなキューブ(立方体)を追加し、インスペクターウィンドウで、Obstacleと名前を付けます。マテリアルを作成してアタッチしておきます。Rigidbodyコンポーネントをアタッチし、Massの値を10000にし、Use Gravityのチェックマークを外し、ConstraintsのFreeze RotationのX,Y,Zにチェックマークを付けます。

ScriptsフォルダにObstacleという名前のスクリプトを作成します。ObstacleスクリプトをゲームオブジェクトObstacleにドラッグ・アンド・ドロップします。ObstacleスクリプトがゲームオブジェクトObstacleにアタッチできているかをインスペクターウィンドウで確認します。
Obstacleスクリプトに以下のコードを記述します。

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

public class Obstacle : MonoBehaviour
{
    private Rigidbody obstacleRigid;
    private float lastTime = 0.0f;
    private float timeInterval = 4.0f;
    private float speed = 100f;
    private float direction = -1.0f;

    void Start()
    {
        obstacleRigid = GetComponent<Rigidbody>();
        lastTime = Time.time;
        direction = -1.0f;
    }

    void FixedUpdate()
    {
        if (Time.time >= lastTime + timeInterval)
        {
            lastTime = Time.time;
            direction *= -1.0f;
            obstacleRigid.velocity = Vector3.zero;
        }

        Vector3 bearForward = obstacleRigid.transform.right;
        Vector3 moveVector = speed * (bearForward * direction);
        moveVector.y = 0;
        moveVector.z = 0;
        obstacleRigid.AddForce(moveVector, ForceMode.Impulse);
    }
}

スクリプトの動作確認をします。

transformメソッドは、transform.rightで水平方向、transform.forwardで奥行き方向、transform.upで上下方向に座標を移動させることができます。

例)奥行き方向の移動

Vector3 bearForward = obstacleRigid.transform.forward;
        Vector3 moveVector = speed * (bearForward * direction);
        moveVector.x = 0;
        moveVector.y = 0;
        obstacleRigid.AddForce(moveVector, ForceMode.Impulse);

例)上下方向の移動

Vector3 bearForward = obstacleRigid.transform.up;
        Vector3 moveVector = speed * (bearForward * direction);
        moveVector.x = 0;
        moveVector.z = 0;
        obstacleRigid.AddForce(moveVector, ForceMode.Impulse);