Last active
February 18, 2021 03:23
-
-
Save gruman/4297162398cf945b6fbc88d19abe9b1b to your computer and use it in GitHub Desktop.
Automatically animate your tiles in Unity. Zoom in and out at random intervals. Use to add some realism to sprites buy have them subtly zoom in and out.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class AnimateObjectTile : MonoBehaviour | |
{ | |
private float speed; | |
public float minSpeed; | |
public float maxSpeed; | |
private float zoomAmount; | |
public float minZoomAmount; | |
public float maxZoomAmount; | |
private Vector3 newSize; | |
private Vector3 originalSize; | |
public bool zoomingOut = true; | |
void Start() | |
{ | |
GetRandoms(); | |
newSize = new Vector3(transform.localScale.x * zoomAmount, transform.localScale.y * zoomAmount, transform.localScale.z * zoomAmount); | |
originalSize = transform.localScale; | |
} | |
void Update() | |
{ | |
if (zoomingOut) | |
{ | |
if (Vector3.Distance(transform.localScale, newSize) > 0) | |
{ | |
transform.localScale = Vector3.MoveTowards(transform.localScale, newSize, Time.deltaTime * speed); | |
} | |
if (Vector3.Distance(transform.localScale, newSize) == 0) | |
{ | |
zoomingOut = false; | |
} | |
} | |
else | |
{ | |
transform.localScale = Vector3.MoveTowards(transform.localScale, originalSize, Time.deltaTime * speed); | |
if (Vector3.Distance(transform.localScale, originalSize) == 0) | |
{ | |
zoomingOut = true; | |
} | |
} | |
} | |
public void GetRandoms() | |
{ | |
speed = Random.Range(minSpeed, maxSpeed); | |
zoomAmount = Random.Range(minZoomAmount, maxZoomAmount); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment