DRY'er version of ItemNudge
PsilentKnight
csharp
a year ago
1.7 kB
10
Indexable
public class ItemNudge : MonoBehaviour
{
private const float NUDGE_STEP_ANGLE = 2f;
private const float ITERATION_PAUSE_DURATION_SECONDS = 0.04f;
private WaitForSeconds pauseDuration;
private bool isAnimating;
private Transform childTransform;
private void Awake()
{
pauseDuration = new WaitForSeconds(ITERATION_PAUSE_DURATION_SECONDS);
childTransform = gameObject.transform.GetChild(0);
}
private void OnTriggerEnter2D(Collider2D other)
{
if (isAnimating) return;
StartCoroutine(Nudge(gameObject.transform.position.x > other.gameObject.transform.position.x));
}
private void OnTriggerExit2D(Collider2D other)
{
if (isAnimating) return;
StartCoroutine(Nudge(gameObject.transform.position.x > other.gameObject.transform.position.x));
}
private IEnumerator Nudge(bool clockwise)
{
isAnimating = true;
float nudgeStepAngleIn = clockwise ? -NUDGE_STEP_ANGLE : NUDGE_STEP_ANGLE;
float nudgeStepAngleOut = clockwise ? NUDGE_STEP_ANGLE : -NUDGE_STEP_ANGLE;
for (int i = 0; i < 4; i++)
{
childTransform.Rotate(0f, 0f, nudgeStepAngleIn);
yield return pauseDuration;
}
for (int i = 0; i < 5; i++)
{
childTransform.Rotate(0f, 0f, nudgeStepAngleOut);
yield return pauseDuration;
}
childTransform.Rotate(0f, 0f, nudgeStepAngleIn);
yield return pauseDuration;
isAnimating = false;
}
}Editor is loading...
Leave a Comment