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
0
Question by odival · Apr 13, 2015 at 08:12 PM · childdistancepitchpercentageinstantiated

Proximity Detection Code not working

Hi guys, I really need help. Is 2h am here and my brain is half dead so I cant, for the life of me, find out whats wrong with this code anymore. Its suposed to find distance between the game object to wich its attached(target), and another game object(player). Then, depending on the that distance it should change the pitch of an AudioSource inside a child in the (player). However, the pich only change when I pratically over the gameobject. So, for about 9/10 of the way, nothing changes in the pitch, but when I'm right there, then the picth suddenly changes (but not to maximum value of Clamp function). What's going on? I'm want the picth to change gradually, I (player) gets closer to (target). =|

ps.: The script is inside a instantiated prefab. thats why I used FindGameObjectWithTag and FindChild. Also, I'm using Unity5.

 using UnityEngine;
 using System.Collections;
 
 public class ProximitySensor : MonoBehaviour {
     public GameObject player;
     private Transform playerTrans;
     private Transform targetTrans;
     public Transform beepTrans;
     public AudioSource beepSound;
     private float distanceCurrent;
 
     void Start () {
 
         targetTrans = transform;
         player = GameObject.FindGameObjectWithTag("Player");
         playerTrans = player.transform;
         beepTrans = playerTrans.FindChild ("beepController");
         beepSound = beepTrans.GetComponent<AudioSource>();
 
     }
 
     void Update () {
                 
         distanceCurrent = Vector3.Distance (playerTrans.position, targetTrans.position);
         beepSound.pitch = Mathf.Clamp ((1 / distanceCurrent), 0.1f, 3.0f);
 
     }
 }

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

2 Replies

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

Answer by odival · Apr 15, 2015 at 05:58 AM

I got help from a guy named Jon at stackexchange at this topic, and that solved the problem.

Below is the code I end up using:

 using UnityEngine;
 using System.Collections;
 
 public class ProximitySensor : MonoBehaviour {
     public GameObject player;
     private Transform playerTrans;
     private Transform targetTrans;
     private Transform beepTrans;
     public AudioSource beepSound;
     private float distanceCurrent;
     private float distanceMax;
     private float distanceMin;
     private float percent;
 
     void Start () {
 
         targetTrans = transform;
         player = GameObject.FindGameObjectWithTag("Player");
         playerTrans = player.transform;
         beepTrans = playerTrans.FindChild ("beepController");
         beepSound = beepTrans.GetComponent<AudioSource>();
         distanceMax = Vector3.Distance (playerTrans.position, targetTrans.position);
         distanceMin = 1.0f;
 
     }
 
     void Update () {
                 
         distanceCurrent = Vector3.Distance (playerTrans.position, targetTrans.position);
         float percent = Mathf.Clamp((distanceCurrent - distanceMin) /
                                     (distanceMax - distanceMin), 0.0f, 1.0f); //Percentage between min and max
         percent = 1.0f - percent; //invert so larger distances decrease pitch
         //beepSound.pitch = beepMin + ((beepMax - beepMin) * percent);
         //beepSound.pitch = 0.1f + ((3.0f - 0.1f) * percent);
         beepSound.pitch = 0.1f + (2.9f * percent);
     }
 }
 


Comment
Add comment · 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
1

Answer by mcapousek · Apr 13, 2015 at 09:36 PM

I'm not an "audiofile" but you shouldn't change pitch so much, even 10% (+|- 0.1) will modify sound heavilly.

Comment
Add comment · 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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to enable children of child game object? 1 Answer

Math - distance to percentage 2 Answers

Increase audio pitch as object gets closer 1 Answer

How to increase the pitch of an audio with respect to distance? 1 Answer

Pitch changes when i get closer to object with audio source 1 Answer


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