Hi, I am not a coder at all and have only taken beginner course that I need to retake. But I am having an issue with river sounds. Of course a river should play a sound when the character gets within a certain distance of the river. I found the script below which I changed from Java to C# but am getting the following errors in the script:
Assets\MyProject\MyScripts\RiverSound.cs(17,18): error CS0266: Cannot implicitly convert type ‘float’ to ‘int’. An explicit conversion exists (are you missing a cast?)
and…
Assets\MyProject\MyScripts\RiverSound.cs(27,26): error CS0266: Cannot implicitly convert type ‘float’ to ‘int’. An explicit conversion exists (are you missing a cast?)
Here is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RiverSound : MonoBehaviour
{
Transform other;
int range;
AudioClip sound;
bool played;
int timing;
int delay = 10;
void Start()
{
played = false;
timing = Time.time;
}
void Update()
{
if (Vector3.Distance(other.position, transform.position) < range)
{
if (timing < Time.time)
{
played = true;
timing = Time.time + delay;
}
}
if (played)
{
AudioSource.PlayClipAtPoint(sound, transform.position);
played = false;
}
}
}
Any help solve the problem would be much appreciated, thanks.
You can add an Inspector field for serializable references by declaring the variable as public.
Here’s an example…
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RiverSound : MonoBehaviour {
// Public
public Transform other;
public int range;
public AudioClip sound;
public int delay = 10;
// Private
private bool played;
private float timing;
void Start() {
played = false;
timing = Time.time;
}
void Update() {
if (Vector3.Distance(other.position, transform.position) < range) {
if (timing < Time.time) {
played = true;
timing = Time.time + delay;
}
}
if (played) {
AudioSource.PlayClipAtPoint(sound, transform.position);
played = false;
}
}
}
Just to add, the script now works fine but it puts me back at the beginning again with the same problem. I’m not sure if this is normal practice but the only way I could get it to work without the above script previously was to add GameObjects with the Sound Behaviour & Source at various intervals along the river.
I thought having the script would solve the range problem which boils down to the fact that the sound begins from the center of the Object only. NOT, the whole length of the object which is what I am ultimately after… so that I can add one sound source/script per river rather than many along the length of the river as I have done to get around the problem.
Well, since you are already using RAM, you might as well consider using “Ambient Sounds” (from the Guys who do Gaia, I have no affiliation other than having pirchased some assets from them and not being disappointed). It’ll sove this and many other soundscape problems for you with a visual Interface.
Thanks I’ll take a look. I used Gaia to create the terrain and enviro for the weather but the enviro sound effects doesnt give as much control as Ambient Sound looks like it does and which I am after. I didnt realize Procedural Worlds had this asset available.