Hello, I have been using the Unity Shooter Engine 2.2, the AI is pretty well implemented but I wanted the music to change to a more ‘action packed’ tracked when the player had been spotted. I am a poor coder and am unable to figure out how to do this though.
For my Enemy AI, I have:
if (!danger CanSeeTarget (target, attackRange)) {
Alertbox.gameObject.SendMessage ("Enemy_Spotted", SendMessageOptions.DontRequireReceiver);
}
1)after the player is spotted, the enemy AI coroutines stop stop working and the enemy stops responding to the player. This makes me think that SendMessage is the wrong command to use
normalmusic does not stop when Enemy_Spotted is triggered. How can I make normalmusic stop/start playing and alertmusic to only trigger once when spotted?
If (!audio.isPlaying) means that it only runs if music isn’t running already. Since normalMusic counts as a music playing, that will never happen. Soo… What you should do is add a check to see what the current track is. var myTrack : int = 0; Also, you can’t change a music clip while the source is playing, so you always need to call audio.Stop() in that case.
If it’s normalMusic, myTrack = 0. Otherwise, myTrack = 1.
This code switches tracks when the enemy sees me, but I have to pause the game before the tracks will switch. I am not sure why, but i’m working on it!
EDIT #4
Okay I made another change and this is pretty close to what I need, the tracks switch just fine! Sometimes the track doubles up and plays on top of itself, though.
#pragma strict
var normalmusic : AudioClip;
var alertmusic : AudioClip;
var myTrack : int = 0;
//audio.Play();
// Wait for the audio to have finished
yield WaitForSeconds (audio.clip.length);
audio.Pause();
function Start () {
myTrack = 0;
audio.clip = normalmusic;
audio.Play();
}
function Update () {
// if(!audio.isPlaying){
if (myTrack == 1) {
audio.Pause();
//audio.Stop();
audio.clip = alertmusic;
audio.Play();
}//}
}
function Enemy_Spotted () {
myTrack = 1;
}
EDIT:
I was able to figure out my first question (send message) by using
Okay so I’ve got it working now (sort of) my only problem is that the default music named ‘VGMTensionLoop’ is playing for like a second in the very beginning but not more. Could someone please help me figure out why?
Thank you
var VGMMainTheme : AudioClip;
var VGMTensionLoop : AudioClip;
var myTrack : int = 0;
//audio.Play();
function Start () {
myTrack = 0;
audio.clip = VGMTensionLoop;
audio.Play();
}
function Update ()
{
// if(!audio.isPlaying){
if (myTrack == 0) {
audio.Stop();
//audio.Stop();
audio.clip = VGMMainTheme;
audio.Play();
}//}
}
function Enemy_Spotted () {
myTrack = 1;
}
function BadGuy_Dead () {
myTrack = 0;
}
Thanks, the default music is playing now but when he notices the player it goes dead silent and when he dies the VGMMainTheme comes on that was supposed to be playing when he’s noticing the player. Sorry about my incompetence, I really appreciate your help
1# When you set the track to 1, it is going to keep trying to switch to VGMMainTheme, even if it’s already playing.
2# You are currently not changing the clip to anything when the track is changed to 0, so the music will not return to normal.
Cheers mate! Sorry it took me so long to answer but it works now. Thank you! Now I just need to figure out where in the AI script I should put the send.message. Now the music changes wen he gets shot and that’s not exactly what I need. Have a look if you want.
Cheers
import RAIN.Core;
var _gunLogic : AIGunLogic;
//var _rival : Rival;
var _dataController : DataController;
var _player : GameObject;
var meleeDamage : float = 10.0f;
var hideWhenDead : boolean = false;
var _ai : RAINAgent;
function Start()
{
_player = GameObject.Find("Player");
_ai = gameObject.GetComponent("RAINAgent");
_dataController = gameObject.GetComponent("DataController");
}
function Update()
{
_ai.Agent.actionContext.SetContextItem.<float>("health", _dataController.current);
_ai.Agent.actionContext.SetContextItem.<float>("ammo", _gunLogic.dataController.current);
var shootAt : GameObject = _ai.Agent.actionContext.GetContextItem.<GameObject>("enemytarget");
if (shootAt != null)
{
_gunLogic._1stShootTo = shootAt.transform;
}
}
function ApplyDamage(damageSource : DamageSource)
{
if ((damageSource.sourceType == DamageSource.DamageSourceType.GunFire)
(damageSource.sourceObjectType != DamageSource.DamageSourceObjectType.Obstacle))
{
_ai.Agent.actionContext.SetContextItem.<float>("gothit", 1.0f);
transform.Rotate(Vector3.up, -45f);
var testforward:Vector3 = transform.forward;
var testright:Vector3 = transform.right;
transform.Rotate(Vector3.up, 45f);
if (Vector3.Dot(testforward, damageSource.fromPosition) > 0)
{
if (Vector3.Dot(testright, damageSource.fromPosition) > 0)
_ai.Agent.actionContext.SetContextItem.<float>("hitdir", 1f); //left
else
_ai.Agent.actionContext.SetContextItem.<float>("hitdir", 4f); //front
}
else
{
if (Vector3.Dot(testright, damageSource.fromPosition) > 0)
_ai.Agent.actionContext.SetContextItem.<float>("hitdir", 2f); //back
else
_ai.Agent.actionContext.SetContextItem.<float>("hitdir", 3f); //right
}
_ai.Agent.actionContext.SetContextItem.<GameObject>("enemytarget", damageSource.sourceObject);
}
_dataController.current -= damageSource.damageAmount;
GameObject.Find("Upper Body").SendMessage("Enemy_Spotted");
}
//@Aether.MessageHandler("FirePrimaryWeapon")
function FirePrimaryWeapon()
{
_gunLogic.FirePrimary("fire-", "empty-");
}
//@Aether.MessageHandler("FirePrimaryWeaponStop")
function FirePrimaryWeaponStop()
{
_gunLogic.FirePrimaryStop("fire-");
}
//@Aether.MessageHandler("ReloadPrimaryWeapon")
function ReloadPrimaryWeapon()
{
_gunLogic.ReloadWeapon();
}
//@Aether.MessageHandler("Die")
function Die()
{
if (hideWhenDead)
{
gameObject.SetActive(false);
GameObject.Find("Upper Body").SendMessage("BadGuy_Dead");
}
else
{
var colliders : Collider[] = gameObject.GetComponentsInChildren.<Collider>();
for (var i:int = 0; i < colliders.Length; i++)
{
if ((colliders[i] != null) !colliders[i].isTrigger )
{
if( typeof( colliders[i] ) == CharacterController )
{
colliders[i].gameObject.SetActive(false);
}
else
{
GameObject.DestroyImmediate(colliders[i]);
}
}
}
}
}
//@Aether.MessageHandler("MeleeAttack")
function MeleeAttack()
{
var target : GameObject = _ai.Agent.actionContext.GetContextItem.<GameObject>("meleetarget");
var damageSource : DamageSource = new DamageSource();
damageSource.appliedToPosition = target.transform.position;
damageSource.damageAmount = meleeDamage;
damageSource.fromPosition = _ai.Agent.Avatar.gameObject.transform.position;
damageSource.sourceObject = _ai.Agent.Avatar.gameObject;
damageSource.sourceObjectType = DamageSource.DamageSourceObjectType.AI;
damageSource.sourceType = DamageSource.DamageSourceType.MeleeAttack;
target.SendMessage("ApplyDamage", damageSource, SendMessageOptions.DontRequireReceiver);
}