Unity 根据物体与摄像机位置缩放物体大小

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SetObjScale : MonoBehaviour
{
    private GameObject setTarget;
    private Vector3 disSize;

    public float DisTance;

    public bool isOn;

    private float local_size;
    // Start is called before the first frame update
    void Start()
    {
        setTarget = this.gameObject;
        local_size = this.transform.localScale.x;
    }

    // Update is called once per frame
    void Update()
    {
        SetLoadSize();
    }

    private void SetLoadSize() {
        disSize = Vector3.one * (Vector3.Distance(Camera.main.transform.position, transform.position) / DisTance);
        if (disSize.x <= local_size)
        {
            return;
        }
        setTarget.transform.localScale = disSize;
        if (isOn)
        {
            setTarget.transform.localScale = new Vector3(setTarget.transform.localScale.x, setTarget.transform.localScale.y, setTarget.transform.localScale.z);
        }
    }
}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容