Quantcast
Channel: Questions in topic: "enemy"
Viewing all articles
Browse latest Browse all 1488

Change music when spotted by Enemy Unity 5.0

$
0
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(); target.transform.GetComponent(); _attackTime = Time.time; _animator = GetComponent(); _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.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); } }

Viewing all articles
Browse latest Browse all 1488

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>