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

Play sound when enemy chase

$
0
0
I try to make whenever enemy is chasing player, a chase theme will play. I've tried the same exact method for sound such as flashlight and it works just fine. But this one didn't work somehow. private AudioSource chaseSound; void Start() { enemyAnim = GetComponent(); target = FindObjectOfType().transform; chaseSound = GetComponent(); } void Update() { //set a range to follow player if (Vector3.Distance(target.position, transform.position) <= maxRange && Vector3.Distance(target.position, transform.position) >= minRange) { FollowPlayer(); } else if (Vector3.Distance(target.position, transform.position) >= maxRange) { ReturnPosition(); } } public void FollowPlayer() { enemyAnim.SetBool("isMoving", true); enemyAnim.SetFloat("xDirection", (target.position.x - transform.position.x)); enemyAnim.SetFloat("yDirection", (target.position.y - transform.position.y)); //current position, target position, speed to move transform.position = Vector3.MoveTowards(transform.position, target.transform.position, speed * Time.deltaTime); chaseSound.Play(); }

Viewing all articles
Browse latest Browse all 1488

Trending Articles