Bugs with Audio and Raycast [C#]

Hello guys, i’ve got two kind of problem here: Scenario: my game is a 3D game where the player is a tank, written in c#, divided in two different phases (first phase, the camera is attached to the turret with the audio listener inside it and you can shoot but never move;

second phase where you move inside 3 different lanes to avoid obstacles, but you can’t shoot).

My problems:

1- I’m using raycast to shoot the enemies in the first phase (please don’t say to change this method, because my boss wants that I use this and I must use it). Enemies have got a collider (player tank too), but there is a problem with the maxDistance parameter that takes Physic.raycast. The problem is that the bullet reaches the enemy, but it not disappears when it hits the collider, it always go a little more forward, and then it recognizes that it hits a collider. It happens when the player fires and hits the enemy and when the enemy fires and hits the player. I think the problem is inside “playerRayEndPos” and “enemyRayEndPos” that you can check inside my code, but I don’t know how to fix it.

My code for that: void Update () { if (enableBullet) { bulletPos = gameObject.transform.p - Pastebin.com

2- I’ve got two bugs with the audio ingame:

2.1) i want that in a precise moment (that’s why I need to do this inside the update function), my game pauses and play an audio clip. When the audioclip is finished playing, the game resumes. I don’t know why this don’t work and I can’t seem to find a solution.

My code for this: //Inside void Update()if (!keepChangePhase) { if (!phaseChanged) { - Pastebin.com

2.2) (always inside update() ) I want that when the player aims to an enemy, a “beep” audioclip plays. I was able to do this and it works, but when I press arrow keys to move the turret of the tank and the beep is still playing, the pitch of that audioclip automatically changes the pitch (if i move to the left the pitch is higher, to the right is lower). I tried to force the pitch to be 1f, but it doesn’t work. Why this?

My code for that: Vector3 fwd = playerBulletSpawnPoint.transform.TransformDirection (Vector3.forwa - Pastebin.com

Thank you in advance guys!

Hi

Regarding first error, I could only see a possible syntax issue and I haven’t checked the logic portion of it.

Is it possible that the 6th line should instead be :

 enemyRayEndPos.z = enemyObject.transform.position.z - bulletPos.z;

Hi

Regarding 2.1 , are you sure that both keepChangePhase and phaseChanged are false ?

If they are indeed false, then the audio will start playing, however both or one of these above variables need to be made true as soon as you start playing the audio, otherwise in the next Update cycle it will again call StopPreviousAudio() and thus stop the previously playing audio.

I dunno the full flow of your logic from the code you’ve shared but something like this could do the trick perhaps :

  • if (!keepChangePhase) {
  • if (!phaseChanged) {
  • phaseChanged = true; // Add this line !!!
  • StopPreviousAudio ();
  • source.clip = clip1;
  • source.Play ();
  • }
  • StartCoroutine (ChangePhase ());
  • }

enemyRayEndPos is already a float, not a vector3. But I don’t know why the bullet doesn’t stop when it collides with a collider, just after some frames it decides to stop.

It was so simple, i feel like a dumb. Thank you so much, it did the trick! Do you know why i get the problem 2.2 ore the problem 1?

I answer myself for the 2.2 question if someone will have the same problem: The solution was to turn down the Doppler Level inside all Audio Sources to 0 (if it’s not set to 0, it causes the pitch change whenever your audio listener changes position. I cannot imagine why people wants a feature like that ahah)