Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by venhip · Jul 26, 2012 at 12:03 AM · triggersoundnpc

How to play sound on trigger enter a distance behind the player?

Hey,

How would I go about making it so that when the player is within the trigger a sound is played X distance behind the player? This next bit may be difficult; the particular part I want to include this in, is in a level that takes place in long streets. Is there any way to make it so that the sound is only played behind the player but within the confines of the street?

The only way i can think to do so is to attach the sound to an invisible NPC that follows the player but cannot go through walls. Would that work?

Thanks.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by aldonaletto · Jul 26, 2012 at 10:49 AM

The "invisible NPC" alternative may cause problems with shots, explosions or other moving objects because the NPC collider will block them.
If the trigger is static, you can use its position/orientation to define the sound origin, and play the sound at that point with AudioSource.PlayClipAtPoint: this function creates a temporary game object with an AudioSource at the specified point, plays the sound and destroys the object automatically. The trigger volume orientation will define the sound direction: if the player enters its front side, the trigger's forward direction will be used; if entering the back side, use the backward direction (trigger script):

var sound: AudioClip; var distance: float = 30;

function OnTriggerEnter(other: Collider){ if (other.tag == "Player"){ var tPlayer = other.transform; // find the direction trigger->player: var dirPlayer = tPlayer.position - transform.position; // get the absolute angle from the trigger forward direction: var angle = Vector3.Angle(dirPlayer, transform.forward); if (angle < 90){ dirPlayer = transform.forward; // player in front of the trigger } else { dirPlayer = -transform.forward; // player behind the trigger } // calculate the point at "distance" behind the player: var point = tPlayer.position + dirPlayer * distance; AudioSource.PlayClipAtPoint(sound, point); } }

Another possibility is to define the sound origin using the player's forward direction, then check if there are no obstacles in between with Linecast (trigger script):

var sound: AudioClip; var distance: float = 30;

function OnTriggerEnter(other: Collider){ if (other.tag == "Player"){ var tPlayer = other.transform; // calculate the point at "distance" behind the player: var point = tPlayer.position - tPlayer.forward * distance; // check if there are no obstacles in between: if (!Physics.Linecast(tPlayer.position, point)){ // free line of sight: play the sound there: AudioSource.PlayClipAtPoint(sound, point); } } }

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image venhip · Jul 27, 2012 at 12:47 AM 0
Share

Will this manage to stay within the street? Also, due to it being a (helpless)horror game there is no problem with guns, ect. Basically the idea is that as you walk down the street all will be quiet, then as you walk into a certain section of streets (with the trigger enveloping them all) you will hear footsteps behind you and if you turn around 3 times then the enemy is 'allowed' to attack. I thought the npc solution may work because I will have to display the model behind the player at the audio source on the third turn around and doing so will give me the game object to attach the model to.

avatar image aldonaletto · Jul 27, 2012 at 01:16 AM 1
Share

Well, in this case the NPC is a better alternative, since you will need it to attack the player anyway. Child the model to it, and set its renderer.enabled property to false to make it invisible. Use OnTriggerEnter/Exit to enable/disable the sound, like this (trigger script - let's call it TriggerScript.js):

var enableSound = false;

function OnTriggerEnter(other: Collider){ if (other.tag == "Player") enableSound = true; }

function OnTriggerExit(other: Collider){ if (other.tag == "Player") enableSound = false; } Enemy script:

var trigger: TriggerScript; // drag the trigger from Hierarchy to here var footsteps: AudioClip[]; // populate this array with footsteps var stepSize: float = 0.7; private lastStep: Vector3;

function Update(){ if (trigger.enableSound){ var dist = transform.position - lastStep; if (dist.magnitude >= stepSize){ audio.PlayOneShot(footsteps[Random.Range(0, footsteps.length)]); lastStep = transform.position; } } ...

avatar image venhip · Jul 27, 2012 at 02:00 AM 0
Share

Thanks for your help, ill do my best to implement it :S lol

avatar image venhip · Jul 27, 2012 at 02:10 AM 0
Share

Ah! By using rendered.enabled = false will that make if(renderer.isVisible) return false too? Because the turning around uses that to detect the NPC

avatar image aldonaletto · Jul 27, 2012 at 03:09 AM 1
Share

Good point - isVisible doesn't work when the renderer is disabled. An alternative could be to create a completely transparent material (use Transparent/Diffuse shader and set the $$anonymous$$ain Color Alpha component to zero) and swap it with the original material when needed (model script):

var visible = false; // controls the visibility var invisible$$anonymous$$at: $$anonymous$$aterial; // drag the invisible material here private var lastVis = true; private var original$$anonymous$$at: $$anonymous$$aterial;

function Start(){ original$$anonymous$$at = renderer.material; }

function Update(){ if (visible != lastVis){ if (visible){ renderer.material = original$$anonymous$$at; } else { renderer.material = invisible$$anonymous$$at; } } }

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Triggering random sound on player 1 Answer

Totally new noob to mecanim and unity itself - Trigger animation? 0 Answers

On trigger , sound play 2 Answers

Activate sound without Pro filters 0 Answers

How can i start a sound when a specific object is players frustrum? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges