Audio only plays if character is moving?

Hello all! I was making my game today in Unity 3D and something really weird came up. When the zombie gets close to the player, it is supposed to play a sound. And it does, only if I am walking toward it. Here is my code:

FOR THE SOUND

var Player : GameObject;
var scary : AudioSource;

function OnTriggerEnter (other : Collider) {

if (other.collider.tag == Player.tag) {

scary.Play();
}
}

AND FOR THE MOVEMENT OF THE ZOMBIE

var Player : Transform;
var MoveSpeed = 6;
var MinDist = 1;
var MaxDist = 10;

function Start()
{

}

function Update ()
{
//Moves towrd player
transform.LookAt (Player);
if(Vector3.Distance(transform.position,Player.position ) >= MinDist) {

transform.position += transform.forward * MoveSpeed*Time.deltaTime;

}
}


Thanks for the help!

  • AngrySc0rpi0n

You mentioned that the sound only plays when the player character walks towards the zombie - is the goal here to make the sound play while the player character is stationary? Your code appears valid. Try implementing the collision detection and AudioSource.Play() on the zombie object instead of the player.

Thanks for replying :slight_smile:
I realized that I had to make the zombie a rigidbody.
But thanks you for replying.