# Unity 5 - Error with code to make pitch changes based on distance

I really need help. It's 2:00 AM here and my brain is half dead so I can't, for the life of me, find out what's wrong with this code anymore. It's supposed to find the distance between the game object to which it's attached (the target), and another game object (the player). Then, depending on the that distance, it should change the pitch of an AudioSource inside a child (in the player). However, the pitch only changes when I'm practically over the gameobject. So, for about 9/10 of the way, nothing changes in the pitch, but when I'm right there the pitch suddenly changes (but not to maximum value of Clamp function).

What's going on? I want the pitch to, at first, stay at 0. But soon after you walk some steps in the right direction, the pitch must change gradually (and in a evenly manner), as (player) gets closer to (target). =|

ps.: The script is inside a instantiated prefab, that's why I used FindGameObjectWithTag and FindChild. Also, I'm using Unity 5.

<!-- language: lang-cs -->

[four blanks]using System.Windows.Controls;
[more indented code]

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);

}
}

• You could Debug.Log(distanceCurrent) to get some idea of the value ranges you're seeing. If the distance never goes below one, the formula you're using will never create a high pitch. – rutter Apr 13 '15 at 19:38
• @rutter Distances are about 3400.774 (its a big map). 1 / 3400.774 gets 2,94. This value increases if distance decrease. Any ideas for a formula where I can get a high pitch while dealing with these kinds of distances? – Odival Quaresma Neto Apr 14 '15 at 2:01

beepSound.pitch = Mathf.Clamp ((1 / distanceCurrent), 0.1f, 3.0f);


Working backwards:

The maximum distance is (1 / 0.1f) = 10.0
The minimum distance is (1 / 3.0f) = 0.33


I suspect you have accidentally ignored the exponent on "2.94"

1 / 3400.774 = 2.94E-4 == 0.00029405 //clamped for a good, long while


Try:

float percent = Mathf.Clamp((distanceCurrent - distanceMinimum) /
(distanceMaximum - distanceMinimum), 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);


This lerps the pitch based on the inverse-lerp of distanceCurrent.

If you prefer a curved function rather than linear:

percent = 1.0f - percent;
percent *= percent; //squared; 1=1 and 0=0, but 0.5 (distance) now equals 0.25 (beep)


or:

percent = sqrt(percent); //square root; 1=1, 0=0, 0.25 = 0.5


• Thanks Jon, that solved the problem beautifully, quite a ingenious piece of code. =) – Odival Quaresma Neto Apr 15 '15 at 2:46