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