Skip to content

Instantly share code, notes, and snippets.

@grifdail
Last active October 13, 2021 12:02
Show Gist options
  • Save grifdail/577a6181eaf01e50f7990305f36c4b57 to your computer and use it in GitHub Desktop.
Save grifdail/577a6181eaf01e50f7990305f36c4b57 to your computer and use it in GitHub Desktop.
using System.Collections;
using UnityEngine;
using UnityEngine.EventSystems;
public class ClickEffect : MonoBehaviour, IPointerClickHandler
{
public float RotationMax = 10;
public float DropMax = 10;
public float Duration = 0.2f;
private Vector3 _origin;
public void OnPointerClick(PointerEventData eventData)
{
var mouse = transform.InverseTransformPoint(eventData.position);
var rect = (transform as RectTransform).rect;
var deltaX = mouse.x - rect.center.x;
var deltaXDistance = deltaX / rect.width * 0.5f;
var angle = -deltaXDistance * RotationMax;
var changeY = (1 - Mathf.Abs(deltaXDistance)) * DropMax;
StartCoroutine(AnimateCoroutine(changeY, angle, Duration));
}
// Start is called before the first frame update
void Start()
{
_origin = transform.localPosition;
}
// Update is called once per frame
IEnumerator AnimateCoroutine(float drop, float angle, float duration)
{
float timer = 0;
do
{
Debug.Log(angle);
float t = timer / duration;
transform.localPosition = Vector3.Lerp(_origin + Vector3.down * drop, _origin, t);
transform.localRotation = Quaternion.Euler(0, 0, Mathf.Lerp(angle, 0, t));
yield return null;
} while ((timer += Time.deltaTime) < duration);
transform.localPosition = _origin;
transform.localRotation = Quaternion.Euler(0, 0, 0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment