StopSounds = false //resets the stopSounds bool so the sound can be played againĭragging the Audio Source components into the newly created list, and altering the strength depending on your chain, will make the chain rattle when the character lands on the platform. StopSounds = true //stops the sounds playing constantlyĮlse if( <= strength & stopSounds) If ( > strength & !stopSounds) //If the force pulling on the hinge is greater than the thresholdĬhainSoundChoice = Random.Range(0, chainSounds.Length) //Choosing a random sound so it's not always the same Hinge = GetComponent() //Sets the hinge variable as the connected HingeJoint2D This will be different depending on the mass and amount of chains.
Public int strength = 300 //The force at which the sound will be played. Private bool stopSounds = false //So the sounds don't constantly play Then create a new C# script called ChainSound and add the following code: using System.Collections To add sound to these platforms, click on the first child link (the first link with a hinge joint) and just like what you did with the character, add multiple Audio Source components with each chain sound as the Audio Clip element, and the Play on Awake checkbox unticked. Just like the other links, add a Hinge Joint 2D component to the platform, setting the Connected Rigidbody to be the link above, and changing the pivot to where it connects to the chain. You also need to check the Freeze Rotation box under Constraints, so that the platform stays flat when the player lands on it.
Next add a Rigidbody 2D component and increase its mass to 5 or something so that it is heavier than the player to give a more realistic swing. Now you have chains that interact with the character and add an extra dimension to your game! Drag this chain into your prefabs folder and you can place them wherever you like. Make sure you uncheck the Play on Awake box so that they don’t play when the game is launched. If (("Chain"))ĬhainSoundChoice = Random.Range(0, chainSounds.Length) Īdd multiple Audio Source components to your character, with each different chain sound, and then drag each one into the new chainSounds list you created. private void OnCollisionEnter2D(Collision2D collision) As you will run into a few chains at the same time, it sounds like the chains are clinking together. In the OnCollisionEnter2D function, we add a check for hitting chains, that when triggered, will play a random chain sound. You will also need a private integer called chainSoundChoice. Now go to the PlayerMovement.cs script and add a new Audiosource list variable and call it chainSounds. I cut each rattle from this clip using Audacity and imported them into the Unity project inside the Sounds folder. There is a very useful audio clip of chains rattling found at.