Skip to content

Instantly share code, notes, and snippets.

@TheCuttlefish
Created December 11, 2020 13:31
Show Gist options
  • Save TheCuttlefish/e2548d92532b5e1a9950bc2ff1c9fe79 to your computer and use it in GitHub Desktop.
Save TheCuttlefish/e2548d92532b5e1a9950bc2ff1c9fe79 to your computer and use it in GitHub Desktop.
Play sound on collision + change colour based on velocity
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