【Unity】入力値を緩やかに変化させる-SmoothDamp
2020-03-26 2020-11-26
これまたUnityの便利な関数.Mathf.SmoothDamp
上記gifの動かしてるコード
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 27 28 29 |
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Smoother : MonoBehaviour { public float InputValue; public float OutPutValue; [Space(15f), SerializeField] private Transform _referenceTransform; [SerializeField] private Transform _targetTransform; [Space(15f), SerializeField] private float _velocity = 0f; [SerializeField] private float _smoothTime = 1f; [SerializeField] private float _maxSpeed = 1f; [Space(15f), SerializeField] private bool _isUseInputTransform; private void Update() { if (_isUseInputTransform) InputValue = _referenceTransform.position.y; OutPutValue = Mathf.SmoothDamp(OutPutValue, InputValue, ref _velocity, _smoothTime, _maxSpeed); _targetTransform.position = new Vector3(0, OutPutValue, 0); } } |
カナメはこの部分
1 |
Mathf.SmoothDamp(OutPutValue, InputValue, ref _velocity, _smoothTime, _maxSpeed); |