Created
October 31, 2023 12:45
-
-
Save TheCuttlefish/555b21a09e08f4d44c0539ea74b6832a to your computer and use it in GitHub Desktop.
Ripple effect in Unity
This file contains hidden or 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 Ripple : MonoBehaviour | |
{ | |
//preset | |
public GameObject square; | |
public Gradient g; | |
public int number = 50; | |
int numHalf; | |
List<GameObject> list = new List<GameObject>(); | |
public float xWave = 1.38f, yWave = 0.26f; | |
public float scale = 1f; | |
Vector3 offset = new Vector3(0,0,10); | |
Vector3 mousePos; | |
float timer; | |
void Start() | |
{ | |
numHalf = number / 2; | |
for (int x = -numHalf; x < numHalf; x++) | |
{ | |
for (int y = -numHalf; y < numHalf; y++) | |
{ | |
GameObject s = Instantiate(square, new Vector2(x , y ), Quaternion.identity); | |
s.GetComponent<SpriteRenderer>().color = g.Evaluate( (Mathf.Cos( s.transform.position.x / xWave + s.transform.position.y / yWave) + 1 ) /2 ); | |
list.Add( s ); | |
} | |
} | |
} | |
private void Update() | |
{ | |
mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition) + offset; | |
timer += Time.deltaTime; | |
foreach (GameObject s in list) | |
{ | |
s.GetComponent<SpriteRenderer>().color = g.Evaluate((Mathf.Cos(timer+ s.transform.position.x / xWave + Mathf.Cos( s.transform.position.y / yWave )) + 1) / 2); | |
s.transform.localEulerAngles = new Vector3 (0f, 0f, 50 * (Mathf.Cos(s.transform.position.x / xWave + Mathf.Cos(s.transform.position.y / yWave)) + 1) / 2); | |
float dist= Vector2.Distance(mousePos, s.transform.position); | |
s.transform.localScale = new Vector3(1,1,1) * Mathf.Cos( dist + timer )/ scale; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment