【Unity】滑らかに値を変化させるクラス
2020-11-22 2020-11-26
個人的メモ。SmoothDampを忘れるのとちょっと使うのが面倒なので作り置き。https://www.kemomimi.dev/unity/1370/
の記事をもっと使いやすくした。

SmoothFloat.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
using UnityEngine; public class SmoothFloat { private float calculatedValue; private float velocity; private float smoothTime; private float maxSpeed; public SmoothFloat(float smoothTime = 1f, float maxSpeed = 1f) { this.velocity = 0f; this.smoothTime = smoothTime; this.maxSpeed = maxSpeed; } public float Calc(float currentValue) { calculatedValue = Mathf.SmoothDamp(calculatedValue, currentValue, ref velocity, smoothTime, maxSpeed); return calculatedValue; } } |
使い方
1 2 3 4 5 6 7 8 9 |
SmoothFloat sf; private void Start{ sf = new SmoothFloat(smoothTime, maxSpeed); } private void Update{ Debug.Log(sf.Calc(targetValue)); } |