Bouncy Bouncy

This commit is contained in:
2025-07-13 22:05:54 -07:00
parent ca0af09aac
commit 8af34427e8
4 changed files with 112 additions and 0 deletions

View File

@@ -0,0 +1,87 @@
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.InputSystem;
public class BouncyButton : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
bool isHovered = false;
bool isHeld = false;
bool didBounce = false;
Vector3 originalScale;
Vector3 finalScale;
void Start()
{
originalScale = transform.localScale;
finalScale = originalScale * 1.1f;
}
public void OnPointerEnter(PointerEventData eventData) => isHovered = true;
public void OnPointerExit(PointerEventData eventData)
{
isHovered = false;
ResetScale();
}
void OnDisable()
{
isHovered = false;
ResetScale();
}
void Update()
{
if (Application.isMobilePlatform)
{
isHeld = Touchscreen.current != null && Touchscreen.current.press.isPressed;
}
else
{
isHeld = Mouse.current != null && Mouse.current.leftButton.isPressed;
}
if (isHovered && isHeld && !didBounce)
{
StartCoroutine(BounceOnce());
didBounce = true;
}
if ((!isHovered || !isHeld) && didBounce)
{
ResetScale();
}
}
void ResetScale()
{
StopAllCoroutines();
transform.localScale = originalScale;
didBounce = false;
}
System.Collections.IEnumerator BounceOnce()
{
float t = 0.125f;
Vector3 s1 = originalScale * 1.12f;
Vector3 s2 = originalScale * 1.06f;
Vector3 s3 = finalScale;
yield return LerpScale(originalScale, s1, t);
yield return LerpScale(s1, s2, t * 0.5f);
yield return LerpScale(s2, s3, t * 0.5f);
transform.localScale = s3;
}
System.Collections.IEnumerator LerpScale(Vector3 from, Vector3 to, float duration)
{
float elapsed = 0f;
while (elapsed < duration)
{
elapsed += Time.deltaTime;
float p = elapsed / duration;
transform.localScale = Vector3.Lerp(from, to, p);
yield return null;
}
transform.localScale = to;
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 200e77fbcd2ab3206aa2fb9665984330

View File

@@ -0,0 +1,21 @@
using UnityEngine;
using TMPro;
public class BouncyText : MonoBehaviour
{
public float frequency = 2f;
public float minSize = 10f;
public float maxSize = 12f;
private TextMeshProUGUI text;
void Start()
{
text = GetComponent<TextMeshProUGUI>();
}
void Update()
{
float newsize = (Mathf.Sin(Time.time * frequency) + 1f) / 2f;
text.fontSize = Mathf.Lerp(minSize, maxSize, newsize);
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 62c57d9ea02aa39eab3ab0d771e2bf55