Created
December 11, 2020 13:31
-
-
Save TheCuttlefish/e2548d92532b5e1a9950bc2ff1c9fe79 to your computer and use it in GitHub Desktop.
Play sound on collision + change colour based on velocity
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 PlaySoundOnCollision : MonoBehaviour | |
{ | |
public AudioSource mySound; | |
public Rigidbody2D rb; | |
float velocityMag; | |
public Gradient myColour; | |
private void Update() | |
{ | |
velocityMag = rb.velocity.magnitude/10; | |
GetComponent<SpriteRenderer>().color = myColour.Evaluate(velocityMag); | |
} | |
private void OnCollisionEnter2D(Collision2D collision) | |
{ | |
mySound.pitch = rb.velocity.magnitude; | |
mySound.Play(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment