Bouncy Bouncy
This commit is contained in:
87
Assets/Scripts/BouncyButton.cs
Normal file
87
Assets/Scripts/BouncyButton.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
2
Assets/Scripts/BouncyButton.cs.meta
Normal file
2
Assets/Scripts/BouncyButton.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 200e77fbcd2ab3206aa2fb9665984330
|
||||||
21
Assets/Scripts/BouncyText.cs
Normal file
21
Assets/Scripts/BouncyText.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
2
Assets/Scripts/BouncyText.cs.meta
Normal file
2
Assets/Scripts/BouncyText.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 62c57d9ea02aa39eab3ab0d771e2bf55
|
||||||
Reference in New Issue
Block a user