- Home /
Scripting error #2!
This is my AI script for the enemies in my game. My problem is that it plays the shoot sound but for some reason it doesn't play the dieSound. Here's my script -
var target; //target = transform.Find("Character"); var damp : int = 3; var attackRange = 3; // safe distance from the player var followRange = 50; //distance required for enemy to start following var bulletPrefab : Transform; var savedTime = 0; var explosionPrefab : Transform; var killsNeeded = 10; var currentLevel = 2; var shootSound : AudioClip; var dieSound : AudioClip;
var hitPoints = 3;
function Update () {
target = GameObject.FindWithTag("Player");
if((followRange > Vector3.Distance(target.transform.position, transform.position )) && (attackRange != Vector3.Distance(target.transform.position, transform.position ))) // if the enemy is closer than the follow range { follow(target); }
var seconds : int = Time.time; var oddeven = (seconds % 2);
if ((attackRange >= Vector3.Distance(target.transform.position, transform.position )) &&(oddeven)) Shoot(seconds); }
function follow(target) { if( attackRange < Vector3.Distance(target.transform.position, transform.position)) //if enemy is farther than attack range { transform.LookAt(target.transform); //look at target transform.Translate (0,0,6*Time.deltaTime, Space.Self ); //move towards target } }
function Shoot(seconds) { if(seconds != savedTime) { audio.PlayOneShot(shootSound); var shoot = Instantiate(bulletPrefab, transform.Find("BulletSpawn").transform.position, Quaternion.identity); shoot.gameObject.tag = "enemyProjectile"; shoot.rigidbody.AddForce(transform.forward * 1000);
savedTime = seconds;
} }
function OnTriggerEnter(hit : Collider) { if(hit.gameObject.tag == "fallout") { Die(); }
if(hit.gameObject.tag == "playerProjectile") { hitPoints -= 1; //Destroy(hit.gameObject);
if(hitPoints < 1)
{
Die();
}
} }
function Die() { audio.PlayOneShot(dieSound); MoveAround.kills++; botMaker.AlienCount--; //MoveAround.scored = true;
if (MoveAround.kills >= killsNeeded) { Application.LoadLevel(currentLevel); healthControl = 6; MoveAround.kills = 0; botMaker.AlienCount = 0; currentLevel++; }
Destroy(gameObject); var explode = Instantiate(explosionPrefab, gameObject.transform.position, Quaternion.identity); }
where is the audio source for the die sound in the scene? it may be because you are to far away with logrithmic roll off on the audio source. Find the audio source, set its roll off to linear and make sure you're really close to the enemy when you kill it. If that doesn't work tell us more I'm sure we will do what we can to help :)
Its not the audio source because i hear the shoot sound when the enemies shoot.
Answer by Bunny83 · Mar 25, 2011 at 01:46 AM
You start playing a sound and in the same frame you destroy the object (including the audiosource). You can either create a temporary GO with an audiosource to play the die sound or you have to delay the Destroy(gameObject);
The Destroy function have a second parameter that allows you to specify a delay in seconds. If the die-sound length is 2 seconds you could do:
Destroy(gameObject,2.2);
Thank you that works. I dont know why I didnt think about that I feel stupid now :(
Don't feel stupid. There are million ways of doing something. And a lot of things are not obvious if you haven't worked with it. Same goes for coroutines. If you delete a GameObject on which a coroutine is running, it will be ter$$anonymous$$ated. $$anonymous$$y way is just a way, not "the" way :D.
Answer by DaveA · Mar 25, 2011 at 01:06 AM
Double check that it is assigned to this script, that it's volume settings are kosher, all that.