Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by gabrielsc18 · Apr 28, 2017 at 05:45 AM · fpsenemymusicshooterenemyai

Change music when spotted by Enemy Unity 5.0

Hey,

I was trying a way to change the music of the game when the enemy sees the player or when he get hurt. Once dead or enemy lose you returns to the original music. It works well, my problem arises when I put more than one enemy in the scene, when I see one of them the music changes, but when the first one dies, the music stops completely instead of continuing and then when I kill the second enemy, Changes to the main music. I am using an eventManager to communicate between classes, I will only copy the enemy class and the music class of the scene.

I hope you can help me, thanks anyway.

Enemy Class  using System.Collections; using System.Collections.Generic; using UnityEngine;

public class Bastard : MonoBehaviour, EnemyAI {

 float _distance;
 public GameObject target;
 public float lookAtDistance;
 public float chaseRange;
 public float attackRange;
 public float followRange;
 public float moveSpeedWalk;
 public float moveSpeedRun;
 public float attackRepeatTime;
 public float attackDamage;
 public CharacterController characterController;
 public float gravity;

 protected float _attackTime;
 Vector3 _moveDirection = Vector3.zero;
 Animator _animator;
 float _initialSpeedRun;
 float _initialSpeedWalk;
 public float maxDistance ;


 bool _isWalking;
 bool _isRunning;
 bool _isAttacking;

 public float health;
 float _maxHealth;
 bool _isDeath;
 AudioSource _audioSource;
 public AudioClip deadSound;
 public AudioClip footStepSound;
 public AudioClip attackSound;
 public AudioClip monsterScreamSound;

 protected virtual void Start()
 {
     _maxHealth = health;
     target = GameObject.Find("Player");
     _audioSource = GetComponent<AudioSource>();
     target.transform.GetComponent<PlayerHealth>();
     _attackTime = Time.time;
     _animator = GetComponent<Animator>();
     _initialSpeedRun = moveSpeedRun;
     _initialSpeedWalk = moveSpeedWalk;

 }


 protected virtual void Update()
 {
     
     _animator.SetBool("isWalking", _isWalking);
     _animator.SetBool("isRunning", _isRunning);
     _animator.SetBool("isAttacking", _isAttacking);       

     if (health <= 0)
     {
         health = 0;           
         _isDeath = true;
     }

     _distance = Vector3.Distance(target.transform.position, transform.position);

     if (_isDeath)
     {
         //play the main music when enemy is dead
         EventManager.instance.DispatchEvent(EventID.MAIN_MUSIC, this.gameObject);
         characterController.enabled = false;
         _animator.SetBool("isDead", true);
         Destroy(gameObject, 20);
     }
     else
     {
         _animator.SetBool("isDead", false);
     }

     if (_distance <= lookAtDistance && !_isDeath)
     {
         LookAtPlayer();
         MaxDistanceToPlayer();
     }
     else{
         health = _maxHealth;
         //play the main music when enemy lose you
         EventManager.instance.DispatchEvent(EventID.MAIN_MUSIC, this.gameObject);
     }

     if (_distance <= followRange && !_isDeath)
     {
         _isWalking = true;
         followPlayer();
     }
     else
     {
         _isWalking = false;
     }

     if (_distance <= attackRange && !_isDeath)
     {

         _isAttacking = true;
         MaxDistanceToPlayer();
         Attack();
     }
     else
     {
         _isAttacking = false;
     }

     if (_distance <= chaseRange && !_isDeath || health < _maxHealth)
     {
         _isRunning = true;
         Chase();
         MaxDistanceToPlayer();
     }
     else
     {
         _isRunning = false;

     }
 }

 public void LookAtPlayer()
 {

     Vector3 direction = target.transform.position - transform.position;
     direction.y = 0;
     transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(direction), 0.1f);

 }

 protected virtual void Attack()
 {
     if (Time.time > _attackTime)
     {

         if (target != null)
         {
             target.SendMessage("ApplyDamage", attackDamage);
             _attackTime = Time.time + attackRepeatTime;
         }
     }
 }

 public void Chase()
 {
     //play the enemy music.
     EventManager.instance.DispatchEvent(EventID.PLAYER_SEEK, this.gameObject);

     _moveDirection = transform.forward;

     _moveDirection *= moveSpeedRun;

     _moveDirection.y -= gravity * Time.deltaTime;

     characterController.Move(_moveDirection * Time.deltaTime);
 }


 public void followPlayer()
 {
     //play the enemy music.
     EventManager.instance.DispatchEvent(EventID.PLAYER_SEEK, this.gameObject);

     _moveDirection = transform.forward;

     _moveDirection *= moveSpeedWalk;

     _moveDirection.y -= gravity * Time.deltaTime;

     characterController.Move(_moveDirection * Time.deltaTime);
 }

 public void ApplyDamage(float damage)
 {
     health -= damage;
 }

 public void MaxDistanceToPlayer()
 {
     if (_distance <= maxDistance)
     {
         moveSpeedRun = 0;
         moveSpeedWalk = 0;
     }
     else
     {
         moveSpeedRun = _initialSpeedRun;
         moveSpeedWalk = _initialSpeedWalk;
     }
 }

 public void playDeadSound()
 {
     _audioSource.PlayOneShot(deadSound);
 }

 public void playFootsteps()
 {
     _audioSource.PlayOneShot(footStepSound);
 }

 public void playAttackSound()
 {
     _audioSource.PlayOneShot(attackSound);
 }

 public void playMonsterScream()
 {
     _audioSource.PlayOneShot(monsterScreamSound);
 }

}

sceneMusic Class

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class levelMusic : MonoBehaviour {

 public AudioClip mainMusic;
 public AudioClip enemyMusic;
 AudioSource _audioSource;
 int myTrack;

 // Use this for initialization
 void Start () {

     myTrack = 1;
     _audioSource = GetComponent<AudioSource>();
     _audioSource.clip = mainMusic;
     _audioSource.Play();
     EventManager.instance.AddEventListener(EventID.PLAYER_SEEK, PlayEnemyMusic);
     EventManager.instance.AddEventListener(EventID.MAIN_MUSIC, PlayMainMusic);

 }
   

 void PlayEnemyMusic(GameObject sender)
 {
     if(myTrack == 1)
     {
     _audioSource.clip = enemyMusic;
     _audioSource.Play();
         myTrack = 0; 
     }
 }

 void PlayMainMusic(GameObject sender)
 {
     if(myTrack == 0)
     {
     _audioSource.clip = mainMusic;
     _audioSource.Play();
      myTrack = 1;
     }
 }

 private void Dispose(GameObject sender)
 {
     EventManager.instance.RemoveEventListener(EventID.PLAYER_SEEK, PlayEnemyMusic);
     EventManager.instance.RemoveEventListener(EventID.MAIN_MUSIC, PlayMainMusic);
    
 }


}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

117 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

UNITY shooting script does nothing 1 Answer

ENEMY STAYS HALF BELOW THE GROUND HALF ABOVE 2 Answers

How can I make a 4 direction animation for an enemy? 1 Answer

Simple Recoil and Reset System 0 Answers

A few tips on FPS design. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges