ShanDongVirtualPowerPlant/u3d-ShanDongVirtualPowerPlant/Assets/Adam/Scripts/Components/DistanceAdjuster.cs

38 lines
1.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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;
}
}