Chapter 9 Robot Rampage Gun Switching Issue

Hi,

I have tried and restarted project and still can not get guns to switch properly. The Pistol shows when running the game but when 2 or 3 is pressed the pistol disappears as expected but the assault rifle or shotgun does not show just a empty space.

The only way I got them to show is by re-selecting the check box next to RobotRampage_AssaultRifle or _Shotgun (children of 2AssaultRifle and 3Shotgun in the hierarchy) though the instructions say to un-check so to have the pistol only shown, once the others are re-checked all 3 appear even when running and do not go away until 1,2 or 3 is pressed. Not the desired functionality/play.

What am I missing as I’m following the book step by step?

Thanks for your time,
Rich

It worked for me - one thing to check

The game objects for the weapons should be unchecked, not the child models

so in Hierarchy

  • Player
    • Camera
      • 1Pistol (checked)
        • RR_Pistol (checked)
      • 2AssaultRifle (UN-checked)
        • RR_AssaultRifle (checked)
      • 3Shotgun (UN-checked)
        • RR_Shotgun (checked)

:+1: thanks that did solve it, not sure how I missed that or had them switched.

I’m in the middle of completing chapter 10, but I am curious if your gameplay is the same as mine as is also in the source code examples. Once ammo runs out I hear a click and fire animation plays but the physical book says on pg 250 (last line): Once you run out of ammo, you should hear the dry-fire and the fire animation should not play. I have gone through the previous chapter (chapter 9), code in the final (chapter 11) and even ran it to play and cannot find the line of code or lines where it stops the animation from playing once all ammo is used up nor does the final version (chapter 11) not play the fire animation with no ammo on three guns. Could that be missing code in the instructions?

Its not a big deal but would make gameplay even better if the gun did not play the fire animate when there is no ammo. And should not sounds like a disclaimer :smile:.

Thanks again for your reply, as I am still newish to unity
Best Regards,
Rich

ok so found a way to stop animation of gun fire once clip is empty.

In the Gun Script, in the Fire method I changed the location of the Fire animation code line as follows:

(disregard the extra ‘//’ and weird spacing throughout code as it was the only way to remove code from being in both text form and scroll box sections)

protected void Fire()
{
// checks if player has remaining ammo, if so play fire
if (ammo.HasAmmo(tag))
{
GetComponent().PlayOneShot(liveFire);
ammo.ConsumeAmmo(tag);
//
// *** copied it here so it only animates when there is ammo and gun is firing *** //
GetComponentInChildren().Play(“Fire”);
//
}
//
// otherwise play empty sound
else
{
GetComponent().PlayOneShot(dryFire);
}
//
// *** grab this line and moved it up -copied & muted, just in case=) *** //
// grabs animator controller and plays Fire animation
//GetComponentInChildren().Play(“Fire”);

After I am more comfortable with Unity, I will play around with a copy of the Fire animation to make an animation that moves like 10% or so of the original animation to mimic an empty clip and the hammer hitting nothing

I think you’re right. To not show the animation, you need the

GetComponentInChildren<Animator>().Play("Fire");

line in the half of the if statement with the liveFire sound. I’ve very unfamiliar with the animation bits, so it will be a while before I fiddle with those.