|
Post by jetjmu on Feb 21, 2020 21:33:44 GMT
I'm very new to unity in general so sorry if this is a stupid question. I am trying to set an "item pickup" sound - Right now I'm trying to use the Item Collection script on the collectable, there are lots of options on the Events tab. I am trying the "Input Events" "On Press Action Input", I've added an event and linked the sound that I want to play in the "object" field. Nothing is happening when I pick the item up, I assume I am going about it the wrong way. Can anyone help? I have no idea what I'm doing..
|
|
|
Post by jetjmu on Feb 23, 2020 19:00:18 GMT
So I've figured it out, I've now got the event linked to an audio manager which is playing the sound just fine. My question now is: Let's say I want to play a sound when the last shot in a clip is fired. So I'd like the gunshot sound to play, along with another sound. (I'm trying to create the M1 garand clip eject sound).
The "Empty Clip" event plays when it's already empty - the "Finish Ammo" event is only playing during my last clip, and it fires on every shot.
I've found the place in the "vShooterWeaponBase" script that calls the audio file for the shot, and I've tried every way I can think of to try to check the number of shots in the clip, but for whatever reason I can't figure it out:
public AudioClip lastShot;
if (ammo != 1) { if (source) { source.Stop(); source.pitch = (Random.Range(0.9f, 1.1f)); source.PlayOneShot(fireClip); } } else { source.Stop(); source.PlayOneShot(lastShot); source.PlayOneShot(fireClip); }
|
|
|
Post by komposite on Feb 26, 2020 16:04:45 GMT
Hey !
Should do more like this =
//new variable to select the audioclip to play
public AudioClip LastBullet;
//later in the code
if (ammo = 1) { source.PlayOneShot(LastBullet) }
Giving you this answer on the fly, but might work, don't know, tell me 😬
|
|
|
Post by jetjmu on Feb 27, 2020 1:36:17 GMT
Thanks for the reply, I figured it out. Forewarning, I am very new to this so this might not be the proper way to do this. The issue with using the ammo int is that ammo is actually the starting amount of ammunition, not the current amount. So the int "ammo" does not actually change with each shot, it appears to be constant. The current ammo in the clip seems to be stored in the vAmmoDisplay class. This is the class that displays the amount of ammo on the UI. So I checked the amount of ammo left in the clip in the vAmmoDisplay class, and if there was one shot left, I set a static bool in the vShooterWeaponBase class called "ping" to true. That way the vShooterWeaponBase class (the script that handles the weapon shot, and effects) knows when there's one bullet left. I then just created a simple if statement, so when ping was true, and if there's a sound linked to the lastShot effect, then play the sound. Here's my code in vAmmoDisplay. In the variables section up top: public bool ping; And in the UpdateDisplay function: public void UpdateDisplay(string text, int id = 0)
{
if (!text.Equals("") && !display.gameObject.activeSelf)
{
display.gameObject.SetActive(true);
}
if (currentAmmoId != id)
{
onChangeAmmo.Invoke(id);
currentAmmoId = id;
}
display.text = text;
if (!text.Equals(""))
{
string ammoString = text;
ammoString = ammoString.Substring(0, ammoString.IndexOf(" "));
if (ammoString == "1")
{
ping = true;
Invector.vShooter.vShooterWeaponBase.ping = ping;
}
else
{
ping = false;
Invector.vShooter.vShooterWeaponBase.ping = ping;
}
}
} Then in vShooterWeaponBase I created the ping variable, and added the public static bool ping, and a place to add the sound effect for the ping. I also added an event on ping, in case I ever needed to add something else here: //In Weapon Settings: public static bool ping;
//In Audio&VFX: public AudioClip lastShotClip;
//In Events: public UnityEvent onShot, onEmptyClip, onLastShot;
//Then, in the HandleShot function, if ping was true, I called LastShotEffect when the weapon was fired.
protected virtual void HandleShot(Vector3 aimPosition)
{
ShootBullet(aimPosition);
ShotEffect();
if (ping)
{
LastShotEffect();
}
}
//Lastly, in the Effects region, I added the code for LastShotEffect:
protected virtual void LastShotEffect()
{
onLastShot.Invoke();
if (source && lastShotClip)
{
//source.Stop();
source.PlayOneShot(lastShotClip);
}
} There was much rejoicing when I finally got it to work
|
|