70 lines
1.8 KiB
C#
70 lines
1.8 KiB
C#
using UnityEngine;
|
|
|
|
public class TransitionSphere : MonoBehaviour
|
|
{
|
|
public float duration = 1f; // òðèâàë³ñòü àí³ìàö³¿ â ñåêóíäàõ
|
|
private Material sphereMat;
|
|
private Color initialColor;
|
|
private Coroutine currentAnimation;
|
|
|
|
void Start()
|
|
{
|
|
// Îòðèìóºìî ìàòåð³àë
|
|
Renderer rend = GetComponent<Renderer>();
|
|
sphereMat = rend.material;
|
|
|
|
// Ïåðåêîíàéñÿ, ùî øåéäåð ï³äòðèìóº ïðîçîð³ñòü (äëÿ URP Lit)
|
|
sphereMat.SetFloat("_Surface", 1); // Transparent
|
|
sphereMat.SetFloat("_Blend", 0); // Alpha Blend
|
|
|
|
initialColor = sphereMat.color;
|
|
}
|
|
|
|
// Âèêëèê ö³º¿ ôóíêö³¿ çàïóñêຠàí³ìàö³þ
|
|
public void PlayAnimation()
|
|
{
|
|
// ßêùî àí³ìàö³ÿ âæå éäå, çóïèíÿºìî ¿¿
|
|
if (currentAnimation != null)
|
|
StopCoroutine(currentAnimation);
|
|
|
|
// Ïî÷àòêîâ³ çíà÷åííÿ
|
|
transform.localScale = Vector3.zero;
|
|
Color c = initialColor;
|
|
c.a = 1f;
|
|
sphereMat.color = c;
|
|
|
|
currentAnimation = StartCoroutine(AnimateSphere());
|
|
}
|
|
|
|
private System.Collections.IEnumerator AnimateSphere()
|
|
{
|
|
float elapsed = 0f;
|
|
Vector3 startScale = Vector3.zero;
|
|
Vector3 endScale = Vector3.one * 100f;
|
|
|
|
while (elapsed < duration)
|
|
{
|
|
elapsed += Time.deltaTime;
|
|
float t = Mathf.Clamp01(elapsed / duration);
|
|
|
|
// Ìàñøòàá
|
|
transform.localScale = Vector3.Lerp(startScale, endScale, t);
|
|
|
|
// Ïðîçîð³ñòü
|
|
Color c = sphereMat.color;
|
|
c.a = Mathf.Lerp(1f, 0f, t);
|
|
sphereMat.color = c;
|
|
|
|
yield return null;
|
|
}
|
|
|
|
// Ô³íàëüíèé ñòàí
|
|
transform.localScale = endScale;
|
|
Color finalColor = sphereMat.color;
|
|
finalColor.a = 0f;
|
|
sphereMat.color = finalColor;
|
|
|
|
currentAnimation = null;
|
|
}
|
|
}
|