About Sound & Animation

(xcygame) #1

Hello handsome administrator! I according to your advice to my reference “MyFunctions” try to write the code,but do not work. Because I am A beginner of c#, I met some problems, about “On Click”, need to consult.

Play Sound (On click):

public void PlaySound(string soundName)
Play Animation(On click):

public void PlayAnimation(string aniName)

Move Object(On click):

I can’t write out the corresponding script, I spent several days to think about let me very headache


Looking forward to your guidance, thank you very much !

(Leslie Young) #2

VinomaGameGlobal.Instance.PlaySound takes more than just a string “name”. Besides, you should not be using it since it is more for internal use and if you make the wrong calls to it you could mess up what sounds are played when your load from a save slot.

Normally, to play sound under Unity you would simply make a Play() to an AudioSource which has a clip on it. But this involves a few things, like knowing how to add variables to your script so you can link to the audio source. It can however be done a various way. You could for example create a script with one function which can be called from external places (like for example OnClick event of a Button) and it simply looks for an AudioSource and Play it.

See docs for background info, https://docs.unity3d.com/ScriptReference/AudioSource.Play.html

The simplest would be to simply do this…

public class MyFunctions : MonoBehaviour
        // properties which will show up in inspector are marked "public"
	public AudioSource sound1;
	public AudioSource sound2;

	public void PlaySound1()

	public void PlaySound2()

Sound1 and Sound2 will show up in the inspector and you can drag-and-drop AudioSources onto them. See Unity docs on how to use this component; https://docs.unity3d.com/Manual/class-AudioSource.html
Then the PlaySound1/2 functions can be used to play the clips placed into those AudioSources. On what object(s) you place the AdioSources are up to you and note that you may place more than one on an object. You could for example place them on the CustomScripts object if you want.

This is just a simple way of doing this and one can do it in may different ways.

As for the animation, again, use the Unity functions to play them. The Vinoma functions expects a very specific way of doing things and was not really made to be an API/Framework. Here too you will want to add properties into which to drag-drop the animators on which the animation will be triggered,
see https://docs.unity3d.com/Manual/Animator.html
and https://docs.unity3d.com/ScriptReference/Animator.html

(xcygame) #3

Thank you for your handsome administrator! You have very detailed problem analysis, at the same time, your patience to solve that taught me a lot! I will go to study what to do next.
:grin: :relaxed: