player see object

hey everyone i want my player see object and play sound with noise…??? (making scary game a “kuntilanak” search in google for the picture)

@Bunny83 I think he means he wants a sound to play when the camera sees a certain object. @lolk, One way would be to use a raycast on the camera. When the raycast hits this object, you can play a sound attached to either the object or the main camera. In this C# example you would want to attach the script to the main camera. Make sure to create a new C# script and paste this code, also make sure to name the script the same as us see inside the script, public class name. For instance, we’ll name this script “RaycastSoundTest”, so inside the script we would name the class public class RaycastSoundTest. Here. This will add an audioSource to the main camera that you attach it to, you will need to drag a gameobject onto the script for the “ObjectWithSoundTrigger” objetc in the inspector. Once this object passes in front of the camera, the audio clip you set in the camera’s audio source will play. This is a very raw, simple example that you will obviously need to tweak to your specific needs, but it functions, so it should give you an idea of what you need to do. @Bunny83 I did UP Vote your comment, since yes, the question needs to be far more specific, and it definitely needs to make more sense lol. I’m hoping my answer is even what lolk is looking for. If not, @lolk, be more specific :smiley:

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(AudioSource))]

public class RaycastSoundTest : MonoBehaviour
{
    public GameObject objectWithSoundTrigger;
    RaycastHit hit;
    Vector3 fwd;
	float hitRange;
	bool canSeeIt = false;
	
	void Start()
	{
	    hitRange = Mathf.Infinity;	
		audio.loop = false;
		audio.playOnAwake = false;
	}

    void Update()
    {    
		fwd = transform.TransformDirection (Vector3.forward);
        if(Physics.Raycast(transform.position, fwd, out hit, hitRange))
        {
			if(hit.collider.gameObject == objectWithSoundTrigger)
			{
				canSeeIt = true;
			}
        }
		if(canSeeIt && !audio.isPlaying)
		{
			audio.Play();
			canSeeIt = false;
		}
    }

}