Skip to content

Instantly share code, notes, and snippets.

@todorok1
Last active December 28, 2020 03:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save todorok1/a1a1aef97745760e80bdc6cd48e82322 to your computer and use it in GitHub Desktop.
Save todorok1/a1a1aef97745760e80bdc6cd48e82322 to your computer and use it in GitHub Desktop.
UnityのMathfでClampを使ったサンプル
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <Summary>
/// MathfのClampの動作を確認するためのクラスです。
/// </Summary>
public class ClampChecker : MonoBehaviour
{
// Clampとして値を制限する範囲を指定します。
readonly float clampMin = 0f;
readonly float clampMax = 10f;
void Start()
{
// 範囲内の値を入力します。
float checkValue = 5f;
ClampTest(checkValue);
// 下限より小さい値を入力します。
checkValue = -5f;
ClampTest(checkValue);
// 上限より大きい値を入力します。
checkValue = 20f;
ClampTest(checkValue);
}
/// <Summary>
/// 引数で指定された値がClampメソッドを通してどの値になるかを確認します。
/// </Summary>
void ClampTest(float value)
{
float clamped = Mathf.Clamp(value, clampMin, clampMax);
Debug.Log($"{value} を入力した結果、Clampを通した値は {clamped} でした。");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment