Audio Source Not Playing Unity3d? - c#

I am trying to play an AudioSource when the trigger is entered by the player but for some reason nothing is working.
I have an Audio Source on the coin that the player will pick up and a sound attached to that audio source.
using UnityEngine;
using System.Collections;
public class DingSoundPlay : MonoBehaviour {
public AudioSource DingAudioSource;
void OnTriggerEnter (Collider other)
{
if (other.tag == "Player")
{
DingAudioSource.Play();
Debug.Log ("Sound Played");
}
}
}
What am I doing wrong??

It might be better for a Coin sound to use the PlayOneShot() method for this, this needs an AudioClip though:
public AudioClip audioClip
DingAudioSource.PlayOneShot(audioClip);

Related

Doors in Unity Mp Networking

I’ve been struggling for the past 8 hours to make my door work in multiplayer. With the code I have, i can use the door in singleplayer and in mp only the host can trigger the door animation and walk through it, other players can neither walk through it or even open it. I figured out i have to connect the door to the network.
I’m using mirror into my project and I’ve tried a lot of fixes but noone seem to work. I’ve tried usinc RPC’s, commands, authority, but can’t get it to work even using chat gpt so i tought to try asking a real human. I ve pasted my working code for single player down below, if anyone has any idea, everything is greatly appreciated.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DoorScript : MonoBehaviour
{
[Header("MaxDistance you can open or close the door.")]
public float MaxDistance = 5;
public AudioClip openSound;
public AudioClip closeSound;
private bool opened = false;
private Animator anim;
private Camera playerCamera;
private AudioSource audioSource;
void Start()
{
// Find the player object by tag and get the camera component from it
GameObject player = GameObject.FindGameObjectWithTag("Bosu");
if (player != null)
{
playerCamera = player.GetComponentInChildren<Camera>();
}
// Get the AudioSource component from the same game object as the DoorScript
audioSource = GetComponent<AudioSource>();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.E))
{
Pressed();
}
}
void Pressed()
{
RaycastHit doorhit;
if (Physics.Raycast(playerCamera.transform.position, playerCamera.transform.forward, out doorhit, MaxDistance))
{
if (doorhit.transform.tag == "Door")
{
anim = doorhit.transform.GetComponentInParent<Animator>();
opened = !opened;
anim.SetBool("Opened", opened);
// Play the open or close sound based on the current state of the door
if (opened)
{
audioSource.PlayOneShot(openSound);
}
else
{
Invoke("PlayCloseSound", 0.6f);
}
}
}
}
void PlayCloseSound()
{
audioSource.PlayOneShot(closeSound);
}
}

Unity: Audio Manager for different scene music

I want to have a background music to play seamlessly from “Scene1” to “Scene2” but I want the music to change if I load “Scene3” or “Scene4” (which should have the same behavior). How can I achieve this?
First your music playing gameobject should be using DontDestroyOnLoad functionality, so the music player does not destroy when the scene changes. If you're not using it, read about it here : https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html
After that, the music player could use SceneManager to decide if it should switch the track or not on scene change.
There is a solution for this in Unity forums SOLUTION
using UnityEngine;
public class MusicClass : MonoBehaviour
{
private AudioSource _audioSource;
private void Awake()
{
DontDestroyOnLoad(transform.gameObject);
_audioSource = GetComponent<AudioSource>();
}
public void PlayMusic()
{
if (_audioSource.isPlaying) return;
_audioSource.Play();
}
public void StopMusic()
{
_audioSource.Stop();
}
}
If you want it to change after just change the audio clip like HERE
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(AudioSource))]
public class ExampleClass : MonoBehaviour
{
public AudioClip otherClip;
IEnumerator Start()
{
AudioSource audio = GetComponent<AudioSource>();
audio.Play();
yield return new WaitForSeconds(audio.clip.length);
audio.clip = otherClip;
audio.Play();
}
}

Playing sound when Player touches part

I have this script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SoundPlayer : MonoBehaviour
{
public Transform Player;
public AudioSource SoundThatPlayed;
void OnTriggerEnter(Collider Player) => SoundThatPlayed.Play();
}
It works perfectly, but the sound plays when ANYTHING touches the part.
OnTriggerEnter fires when ANY collider enters the trigger. You can check for a specific tag, component, name, layer, etc. to filter out the unwanted triggers.
void OnTriggerEnter(Collider collider)
{
var playerScript = collider.GetComponent<Player>();
if (playerScript)
{
// An object with the Player script on it entered the trigger
//
}
var isPlayerByName = collider.name == "My Player Name";
var isPlayerByTag = collider.tag == "My Player Tag";
}

I can't freeze my camera by a script even though it shows that it's freezing all the axis

I was trying to find the answer for the last 3 hours and I couldn't find anything that would help me. It shows that the camera already has constraints when it enters the cube, so it works, but it doesn't because it's not freezing the camera. I don't know what to do already.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LCOE : MonoBehaviour
{
public GameObject cam;
Rigidbody rig;
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
cam.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeAll;
}
}
private void OnTriggerExit(Collider other)
{
if (other.tag == "Player")
{
cam.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.None;
}
}
}
Changing constrains or isKinematic on Rigidbody will only disable physics movement, but it will still move together with parent object. Your camera should not be child of player and script attached to camera should look like that:
class MyCamera : MonoBehaviour{
public Transform target;
public Vector3 offset;
public bool isMovementDisabled;
void LateUpdate(){
if(isMovementDisabled)
return;
transform.position = target.position + offset;
}
}
Or use cinemachine

Unity Unexpected Sound Behavior

Im taking a Unity C# course on Udemy, and we got to an challenge and i came up with a very simple "game". My problem is i can't figure out why no sound is playing? I looked at the instructors code and is the same as my (or did i just miss something?)
When i press on space the rocket starts flying (this works) and should play sound (it doesnt). Yes i got a SoundListener attached to the main camera.
Can somebody help and explain me where i missed something?
Here is my code that i wrote:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Rocket : MonoBehaviour
{
Rigidbody rigidBody;
AudioSource audioSource;
// Use this for initialization
void Start()
{
rigidBody = GetComponent<Rigidbody>();
audioSource = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update()
{
ProcessInput();
}
private void ProcessInput()
{
if (Input.GetKey(KeyCode.Space)) // can thrust while rotating
{
rigidBody.AddRelativeForce(Vector3.up);
if (!audioSource.isPlaying) // so it doesn't layer
{
audioSource.Play();
}
}
else
{
audioSource.Stop();
}
if (Input.GetKey(KeyCode.A))
{
transform.Rotate(Vector3.forward);
}
else if (Input.GetKey(KeyCode.D))
{
transform.Rotate(-Vector3.forward);
}
}
}
Solved it after after declaring the audioSource to public

Categories

Resources