Search Unity

Audio only plays if character is moving?

Discussion in 'Audio & Video' started by AngrySc0rpi0n, Dec 26, 2014.

  1. AngrySc0rpi0n

    AngrySc0rpi0n

    Joined:
    Dec 21, 2014
    Posts:
    7
    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
     
  2. Veemix

    Veemix

    Joined:
    Dec 5, 2014
    Posts:
    25
    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.
     
  3. AngrySc0rpi0n

    AngrySc0rpi0n

    Joined:
    Dec 21, 2014
    Posts:
    7
    Thanks for replying :)
    I realized that I had to make the zombie a rigidbody.
    But thanks you for replying.