スモールライトを用意する
まずスモールライトをモデリングします。
こちらの動画を参考に作成しました。
【blender初心者】ドラえもん・スモールライトを超簡単モデリング!
https://www.youtube.com/watch?v=VlsbqZXfC-U
球のコライダーを2つTrigger状態でつけます。
RigidBodyも取り付けてください。
縮めたいオブジェクトに取り付けるスクリプト
Smallable.cs
using UnityEngine;
public class Smallable : MonoBehaviour
{
// 最小サイズ
public Vector3 MinScale = Vector3.one;
// 縮んでいく比率
public Vector3 ScaleRate = Vector3.one * 0.001f;
}
Cubeなどに取り付けてください。
縮めるスクリプト
SmallLightScaler.cs
using UnityEngine;
public class SmallLightScaler : MonoBehaviour
{
void OnTriggerStay(Collider other)
{
var smallable = other.gameObject.GetComponent<Smallable>();
if(smallable == null)
{
return;
}
var scale = smallable.transform.localScale;
if(scale == smallable.MinScale)
{
return;
}
scale.x = Mathf.Max(smallable.MinScale.x, scale.x - smallable.ScaleRate.x);
scale.y = Mathf.Max(smallable.MinScale.y, scale.y - smallable.ScaleRate.y);
scale.z = Mathf.Max(smallable.MinScale.z, scale.z - smallable.ScaleRate.z);
smallable.transform.localScale = scale;
}
}
このスクリプトをスモールライトに取り付けます。
Smallableを取り付けたオブジェクトに当たっている場合、縮まります。
結果
おわり
応用でビッグライトも作れます。