- Home /
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);
}
}
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);
}
}
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.