Contents
【2DSTG制作】の動画内で表示されるコード:#31 – #40
サムネイルをクリックすると、YouTubeの動画ページに移動します。
#33:レーザーが通過するルートを表示
TrailRendererで事前にルートを表示
// 一定時間後に発射されるレーザー
[SerializeField] GameObject laser;
// エフェクト(Trail Renderer)の移動速度
[SerializeField] float speed;
// エフェクトの始点
Vector3 startPos;
void Start()
{
startPos = transform.position;
// レーザー発射
StartCoroutine(LaserFiring());
}
void Update()
{
// 画面左に進む(エフェクト)
transform.position -= transform.right * speed * Time.deltaTime;
}
IEnumerator LaserFiring()
{
// 回避するまでの猶予
yield return new WaitForSeconds(3.0f);
// エフェクトの開始位置からレーザーを発射
Instantiate(laser, startPos, transform.rotation);
// レーザーが通過するまで待機して削除
yield return new WaitForSeconds(1.0f);
Destroy(gameObject);
}
#35:マウスオーバーでテキストの内容を変更
message window
// TextMeshProを扱う際に必要
using TMPro;
// メッセージウィンドウに表示するテキスト
[SerializeField] TextMeshProUGUI shopText;
// マウスオーバーでテキストの内容を変更
public void OnPointerButton(string button)
{
switch(button)
{
case "Speed":
shopText.SetText("プレイヤーの移動速度が上昇します。");
break;
case "Slash":
shopText.SetText("斬撃を飛ばすスキルが使用可能になります。");
break;
case "Attack":
shopText.SetText("プレイヤーの攻撃力が上昇します。");
break;
default:
break;
}
}
// カーソルがアイコンから離れた時の処理
public void ResetText()
{
// テキストをデフォルトの内容に戻す
shopText.SetText("いらっしゃいませ。\n何をお求めですか?");
}
#36:Live2Dモデルの当たり判定
Live2DモデルのパーツとGameObjectを連動させる
// GameObjectが追従するアートメッシュ
[SerializeField] CubismRenderer _cubismRenderer;
// 座標の調整用
[SerializeField] float adjustX;
[SerializeField] float adjustY;
void Update()
{
var mesh = _cubismRenderer.Mesh;
// メッシュを囲う矩形の中心
Vector3 meshPosition = mesh.bounds.center;
// 追従する座標の調整
meshPosition.x = meshPosition.x + adjustX;
meshPosition.y = meshPosition.y + adjustY;
// 指定したパーツの座標に追従
transform.localPosition = meshPosition;
}
#40:ポストプロセスの操作Part2
ColorAdjustmentsのパラメータを操作
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
using DG.Tweening;
// Volumeコンポーネント
[SerializeField] Volume volume;
// パラメータを操作するエフェクト
ColorAdjustments colorAdjustments;
void Start()
{
volume.profile.TryGet(out colorAdjustments);
}
// 1秒かけて彩度の値を-100(モノクロ)にする
public void Monochrome()
{
DOTween.To(
() => colorAdjustments.saturation.value,
num => colorAdjustments.saturation.value = num,
-100f, 1.0f
)
.SetUpdate(true); // timeScaleを無視する
}
// 彩度を初期値に戻す(エフェクト解除)
public void Color()
{
DOTween.To(
() => colorAdjustments.saturation.value,
num => colorAdjustments.saturation.value = num,
0, 1.0f
);
}





