Quantcast
Channel: Questions in topic: "lockcursor"
Viewing all articles
Browse latest Browse all 92

Need to play lock-on sound only once when crosshair placed onto enemy

$
0
0
Hi, I have a script which adds the enemies within a trigger to an array, finds the closest one to the player and then places the crosshair onto them and fires in their direction. What I would like to do is play a 'Lock-on' sound when the crosshair is placed onto a new enemy. Now, the sound must only be played once when locking onto onto a new enemy. The issue is that in order to stay locked onto the enemy as they move, commands have to be placed into/executed from the Update() function of a script. I need a way to say that when targeting an enemy, play the sound once (so far the sound is playing constantly for the above reasons), so I'm not sure where to call this from. I've tried using flags out and inside of if- statements but this cannot be inside an Update() as this is called multiple times. I have a feeling I'm missing something very simple here :s I have a script dedicated to sounds with a function called PlayLockOnSound() attached to the Player object which simply contains the command: audio.PlayOneShot(lockOnSound); The gist of the else statement inside the script's Update is as follows, this is attached to a Box Collider(Is Trigger): // Otherwise, the array must have something inside it (i.e enemies) else { for (int i = 0; i < enemyCounter; i++) { Debug.Log("***" + enemyArray[i]); // Access the CrosshairToggle script and Enable it GetComponent().enabled = true; // Assign the current enemy's position to the global variable EnemyPosition EnemyPosition = enemyArray[i].transform.position; // Call the FindClosestEnemy() function to find hich of the enemies in the enemyArray (the tube) // is the closest to the player. FindClosestEnemy(); // Access the script's EnemyPos variable and pass it the position of the enemy GetComponent().EnemyPos = EnemyPosition; // Find the BulletSPawner object GameObject bulletDirObj = GameObject.Find("BulletSpawner"); // Access the object's Shooter script and assign the value in bulletDirection to bulletDir here Vector3 bullerDir = bulletDirObj.GetComponent().bulletDirection; bool playOnce = false; // WHAT TO DO HERE ????? if (!playOnce) { GameObject soundObj = GameObject.Find("Player"); soundObj.GetComponent().PlayLockOnSound(); playOnce = true; } } }

Viewing all articles
Browse latest Browse all 92

Trending Articles



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