38 lines
1.1 KiB
C#
38 lines
1.1 KiB
C#
using UnityEngine;
|
||
//============================================================
|
||
//支持中文,文件使用UTF-8编码
|
||
//@author Adam
|
||
//@create 20221005
|
||
//@company Umawerse
|
||
//
|
||
//@description:
|
||
//============================================================
|
||
|
||
public class DistanceAdjuster : MonoBehaviour
|
||
{
|
||
private float minDistance = 10;
|
||
private float maxDistance = 120;
|
||
private float minScale = 0.3f;
|
||
public float maxScale = 1;
|
||
private Camera _camera;
|
||
// Use this for initialization
|
||
private void Start()
|
||
{
|
||
_camera = Camera.main;
|
||
}
|
||
|
||
private void Update()
|
||
{
|
||
var d = Vector3.Distance(transform.position, _camera.transform.position);
|
||
var s = Remap(d, minDistance, maxDistance, minScale, maxScale);
|
||
s = Mathf.Max(s, minScale);
|
||
s = Mathf.Min(s, maxScale);
|
||
transform.localScale = Vector3.one * s;
|
||
}
|
||
|
||
private float Remap(float value, float inMin, float inMax, float outMin, float outMax)
|
||
{
|
||
return (value - inMin) / (inMax - inMin) * (outMax - outMin) + outMin;
|
||
}
|
||
}
|