I am new to game development and building Endless Runner game, where Rocky(Player) can collect coins, and I applied cool sound effect when coin is being collected by player but that is not played. Here is the detail what I did so far,
First I create an Empty Object named it CollectCoin and drop coin collect sound on it.
Inspector View of CoinCollect, as can see CoinSFX is applied on AudioClip property
Coin Object look like this at inspector view and my AudioSource applied there in scripts. Also check Is Trigger checkbox
Player Inspector View
And finally my Class
public class CoinCollect : MonoBehaviour
{
public AudioSource coinFX;
public void OnTriggerEnter()
{
coinFX.Play();
this.gameObject.SetActive(false);
}
}
What and where i done wrong so far? I am stuck here since yesterday. Please help me.
There was nothing wrong with my script, and I dragged both Coin Collect Sound and Background Music at correct place, problem was with my Unity, I don't know when I muted "Mute Audio" option in unity, or its muted by default i have no idea but my problem is resolved. That is why my sound was not playing
Related
I'm trying to make a game where you're a boy and you can transform into a deer.
I don't know how to make the player transform into a deer when pressing a button. Can someone please tell me how to make the player transform into a deer when pressing a button and how to make the deer transform back into the player when pressing the button again?
There are 2 ways depending on what you want exactly.
1.Transformation without an animation, you can do this by having 2 separate models
public GameObject boy;
public GameObject deer;
//Give both objects the same tag and don't add anything else under the
tag in order for this to work
private Transform currentPosition;
private void Start()
{
deer.SetActive(false);
}
public void TransformButton()
{
currentPosition = GameObject.FindObjectWithTag(LayerMask.NameToLayer("TransformationTag"));
//Unity doesn't pick up disabled GameObjects so it will pick up only the
//active state (the active object)
boy.SetActive(!activeInHierarchy);
deer.SetActive(!activeInHierarchy);
GameObject.FindObjectWithTag(LayerMask.NameToLayer("TransformationTag")).getComponent<Transform>() = currentPosition;
}
I recommend this way if your deer and boy have different scripts
you could also play an animation on click (apply this animation to the active character) and when it is done (below is how to check if it is done) you can trigger the code above
https://answers.unity.com/questions/362629/how-can-i-check-if-an-animation-is-being-played-or.html
I am pretty sure there are other better ways but this is the way I do it
The other way which is probably easier if you are good at animating is to just trigger an animation using the animation bools when the button is clicked but I haven't tried this way because I'm not that good at animation. Instead I use the first way (usually for switching skins in a shop for example) but I never did animations that completely change the size ratio so much so I don't know how well would it work (I usually do 1:1 transformations like humanoid to humanoid or tank to tank so you will have to experiment a bit)
I dont think that this is possible in unity. Maybe there are some Assets that offer something like this.
But you could create a similar effect with Blender.
Check out Morphing Shape Animations.
There are some cool tutorials out there Morphing Shape Animations in Blender
I also have a bunch of other Objects in the scene (that aren't instantiated prefabs) which can play sound perfectly like this:
shoot.Play();
where shoot is a public AudioSource, in which i atributed in the Inspector by dragging and dropping an Audiosource of the sound effect, which i made by dragging and dropping a .wav file into the hierarchy.
I couldn't do the same for the prefab, obviously, so i made the AudioSource its child like this:
The AudioSource, as the Prefabs child (imgur image link)
I then dragged and dropped in the Inspector, as the value of the public AudioSource. I tried to play the sound the same way using hitHurt.Play();, But it didnt work.
Someone told me to do it like this:
GetComponent<AudioSource>().Play();
Still won't work
¯\_(ツ)_/¯
edit: nvm fixed lolz
First time developer here and I am struggling with a problem.
I made a mock up of the problem in regular Unity 3d since the VR scares some talented people away from helping me. That can be downloaded here: https://gofile.io/d/qHDlUZ
File is a zip called: SoundTestTroubleShooting. A Unity build on 24.7MB
Follow the number on the buttons.
Button 1 "Choose Music" Activates the Cube.
Button 2/4 "Play Music" Checks if cube is activated and plays song if that is the case.
Button 3 "Restart Scene" Stops the music and reloads the scene.
Cube has dontdestroyonload.
After that i press button 2/4"PlayMusic" and it cant find that the cube is active.
WHY?! Going nuts here.
Old text with the entire problem here:
In my game I have four songs in the start menu that my player gets to choose from. He chooses one by pressing one of four buttons. This activates an empty GameObject, lets call it SmileMusicTrigger. SmileMusicTrigger has a dontdestroyonload and is deactivated by the other three buttons if player wants to switch song.
Player press Begin which moves him into position. He presses start and this button has:
public GameObject SmileMusicTrigger;
public GameObject EpicMusicTrigger;
public GameObject GodMusicTrigger;
public GameObject VikingMusicTrigger;
void OnTriggerEnter()
{
if (SmileMusicTrigger.activeSelf)
{
AudioManager.instance.Play("Smile");
}
if (EpicMusicTrigger.activeSelf)
{
AudioManager.instance.Play("Epic");
}
if (GodMusicTrigger.activeSelf)
{
AudioManager.instance.Play("God");
}
if (VikingMusicTrigger.activeSelf)
{
AudioManager.instance.Play("Viking");
and my game start and and the music begins fine.
When player dies he restarts the scene with restart button which has this to say about the music:
void OnTriggerEnter()
{
AudioManager.instance.Stop("Smile");
AudioManager.instance.Stop("Viking");
AudioManager.instance.Stop("God");
AudioManager.instance.Stop("Epic");
}
}
after this code has run the scene restarts and it does not seem like my Start button can check the active state of SmileMusicTrigger again. I know SmileMusicTrigger is active through the restart of the scene because I see it under dontdestroy in the hierarchy when game is running and it is checked as on in inspector. But I cant seem to call it again. Thus my music only plays once.
Audiomanager is also under Donotdestroy so I dont think the problem is there.
Sorry if this is messy. I am working really hard to get this working but I am so stuck right now.
Thankful for any help.
All the best!
I am trying to make an AR game and am currently using Unity's high-level networking classes. I have set my player prefab to be spawned in one of the two network spawn locations, which are both childs of the Ground Plane Stage. When user has tapped to make their ground plane stage and has tapped to be the host, their player character appears. Unfortunately, if they press the fire button, the bullets appear above the stage and unscaled, meaning they aren't parented to the stage. This confuses me because I've checked many times and the bullet emitter is a child of the player, and in my code it references said emitter. Thus I'm rather confused why the bullets don't seem parented.
I've attempted to attach a script to make the bullet emitter a child of the player when it spawns. I've also tried making it a child of the stage when it spawns. I've tried making the player character not dependent on the Network manager spawning it when the player joins, but then that leads to other networking problems when it comes to controlling the character, but it can shoot then.
The only one that was relatively successful was making the bullet a child of the stage when it spawned, but it would only stay in one place. Attempting to make the bullet a child of the player did nothing
//This is the class I'm trying to use to make the object a child of
something
public class AddToBeetle : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
GameObject player =
GameObject.FindGameObjectWithTag("Player");
transform.SetParent(beetle.transform, false);
}
}
It rarely prints any error messages. I hope that I can eventually get the bullet to spawn in front of the player model when the button is pressed.
Oh gosh I was dumb, the one thing I didn't apply the script to was the player's bullet emitter. It was super late and that's probably why I missed trying it on that. Anyway I hope this helps someone else if they ever have to deal with AR and Unity.
So I did (in Unity editor):
GameObject -> Create Empty
the I dragged my Avatar.cs (the main player animation MonoBehaviour) onto the gamefile.
Inside Avatar.cs I added:
public AudioClip audioClipJump;
and
// this is where the jump animation gets played
audio.PlayOneShot(audioClipJump);
Back in Unity editor, I see the component Audio Clip Jump inside GameObject.Avatar, and I drag and dropped the .wav file onto it.
Then I go into the game, and the jump animation plays, but I have no sound :(
here is a screenshot of the Unity editor:
and like I said, just the 2 lines in the C# file
Check that there is a AudioListener in the scene, probably attached to the Camera.
Try using the second argument on the PlayOneShot function, to indicate the volume.
Remember that having and AudioListener in the same GameObject containing the AudioSource doesn't work (for some reason).
If all fails, try playing the sound from another Component, which should only have the code for playing the sound.