Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Andy47uk · May 03 at 06:08 PM · multiplayerammoguns

my son is doing a multiplayer fps and is having a problem when each player picks up a gun and when one player shoots the other players gun fires to

my son has made a 2 player fps game and is having trouble when each player picks up a pistol and when one of them fires their gun the other players gun fires, also whoever picks up their gun first has the ammo displayed and the other one doesn't. he has autism and is getting really stressed as he has to hand his work in this friday. please help

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Andy47uk · May 04 at 06:32 AM 0
Share

I’m going to get him to post his code what he has done later today. Thank you for all your replies

4 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by zjebali · May 03 at 06:27 PM

So based off the information you've shared it seems like somewhere in his code he is calling a method, say ShootGun() and it's not checking who is making that call thus calling it for all players.

Seems like he needs to add some checks to see who is making the call and make sure that when someone picks up the gun is not calling that same method on both players but only on one.

Going to be hard to provide more help than that.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
2

Answer by Monsoonexe · May 04 at 12:24 AM

Some multiplier solutions like PUN and UNET have a property called IsLocalPlayer. Since the code is running on all scripts on all machines, checking if (IsLocalPlayer) can help you filter code so only the machine whose keyboard was pressed runs that code.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by MickyX · May 04 at 02:33 PM

I think this is happening because the guns don't have any idea who "owns" them

In your code you have both

 if (Input.GetMouseButtonDown(0) && player.hasWeapon)

and

 if (player2.usingController)
          {
              if (triggerState == 0)
              {
                  if (Input.GetAxisRaw("Fire") == 1 && player2.hasWeapon)
                  {

Its highly likely that those two statements could be true even if the gun it held by the other player and therefore it will fire when the other player presses fire.

You need to check if the Gun is owned by the person pressing fire. Something simple like setting a bool to true if player1 picks up the gun and a bool for if player 2 picks up the gun should work

Then you can do an ownership test as part of your logic. i.e, lets say you create a bool of player1PickedUp and set that to true when the gun is picked up now you can check when checking the GetMouseButtonDown if it is player 1 who owns the gun and if so fire.

  if (Input.GetMouseButtonDown(0) && player.hasWeapon && player1PickedUp)


Hope that helps?

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Andy47uk · May 04 at 05:24 PM 0
Share

Hi thanks for your reply he has tried this now and the second player now shoots on his own but player one still fires both guns when firing

avatar image MickyX Andy47uk · May 04 at 06:32 PM 1
Share

If he used that logic for player 1 and it worked then something must be missing from the logic for player 2 it probably needs adding here

 if (player2.usingController)

Additionally probably needs adding to these if statements also as I imagine it is updating both UIs at the same time

 if (player.hasWeapon)
          {
              cam = player.GetComponentInChildren<Camera>();
              HealthBar.ShowAmmo();
              HealthBar.SetAmmo(gun.instance.ammo / gun.instance.maxAmmo);
              HealthBar.SetAmmoText(gun.instance.ammo.ToString());
          }
          else if (player2.hasWeapon)
          {
              cam = player2.GetComponentInChildren<Camera>();
              HealthBar.ShowAmmo2();
              HealthBar.SetAmmo2(gun.instance.ammo / gun.instance.maxAmmo);
              HealthBar.SetAmmoText2(gun.instance.ammo.ToString());
          }
avatar image MickyX Andy47uk · May 04 at 06:32 PM 0
Share

Post the full code again, and I can take a quick scan

avatar image josh26220 MickyX · May 04 at 07:19 PM 0
Share

Hey, I managed to fix the issue!

Turns out it was an external issue and was due to one of the inputs in the Input Manager being set to both the mouse button and the joystick trigger causing it to fire both guns, however your code suggestions definitely helped because without it I think this issue would've still been prevalent.

Thank you so much for the suggestions though, you're a godsend.

Show more comments
avatar image
0

Answer by josh26220 · May 04 at 02:04 PM

Hello, I'm his son.

To further clarify, the game is split screen multiplayer only, and I'm not using any of Unity's Networking services.

Here is the full code to my Weapon class, which is attached to the weapons themselves:

 public class Weapon : MonoBehaviour
 {
     public Camera cam;
     public ParticleSystem muzzle;
     [SerializeField] public Gun gun;
     public GameObject currentGun;
 
     private Player player;
     private Player2 player2;
     private float timeToFire = 0f;
     private int triggerState = 0;
 
     // Start is called before the first frame update
     void Start()
     {
         player = FindObjectOfType<Player>();
         player2 = FindObjectOfType<Player2>();
 
         currentGun = gameObject;
 
         gun = Gun.CreateInstance(currentGun.name, currentGun, 10, 100, 10, 20, 36, false) as Gun;
         gun.instance = gun;
 
         HealthBar.SetAmmo(0);
         HealthBar.HideAmmo();
         HealthBar.SetAmmo2(0);
         HealthBar.HideAmmo2();
 
     }
 
     // Update is called once per frame
     void Update()
     {
         if (player.hasWeapon)
         {
             cam = player.GetComponentInChildren<Camera>();
             HealthBar.ShowAmmo();
             HealthBar.SetAmmo(gun.instance.ammo / gun.instance.maxAmmo);
             HealthBar.SetAmmoText(gun.instance.ammo.ToString());
         }
         else if (player2.hasWeapon)
         {
             cam = player2.GetComponentInChildren<Camera>();
             HealthBar.ShowAmmo2();
             HealthBar.SetAmmo2(gun.instance.ammo / gun.instance.maxAmmo);
             HealthBar.SetAmmoText2(gun.instance.ammo.ToString());
         }
 
         if (player2.usingController)
         {
             if (triggerState == 0)
             {
                 if (Input.GetAxisRaw("Fire") == 1 && player2.hasWeapon)
                 {
                     triggerState = 1;
                     if (!gun.instance.isAutomatic) { Fire2(); }
                     else
                     {
                         if (Time.time >= timeToFire)
                         {
                             timeToFire = Time.time + 1f / gun.instance.fireRate;
                             Fire2();
                         }
                     }
                 }
             }
             else
             {
                 if (Input.GetAxisRaw("Fire") == 0) { triggerState = 0; }
             }
         }
 
         if (Input.GetMouseButtonDown(0) && player.hasWeapon)
         {
             if (!gun.instance.isAutomatic) { Fire(); }
             else
             {
                 if (Time.time >= timeToFire)
                 {
                     timeToFire = Time.time + 1f / gun.instance.fireRate;
                     Fire();
                 }
             }
         }
     }
 
     public void Fire()
     {
         if ((player.hasWeapon) && gun.instance.ammo > 0f)
         {
             muzzle.Play();
 
             if (Physics.Raycast(cam.transform.position, cam.transform.forward, out RaycastHit hit, gun.instance.range))
             {
                 Target target = hit.transform.GetComponent<Target>();
                 if (target != null)
                 {
                     print(target.name + " takes " + gun.instance.damage + " damage");
                     target.TakeDamage(gun.instance.damage);
                 }
             }
 
             gun.instance.ammo--;
             if (gun.instance.ammo < gun.instance.maxAmmo / 4)
             {
                 UI.lowAmmoText.text = "Low Ammo";
             }
             else
             {
                 UI.lowAmmoText.text = "";
             }
         }
         else
         {
             UI.lowAmmoText.text = "No Ammo";
         }
     }
 
     public void Fire2()
     {
         if ((player2.hasWeapon) && gun.instance.ammo > 0f)
         {
             muzzle.Play();
 
             if (Physics.Raycast(cam.transform.position, cam.transform.forward, out RaycastHit hit, gun.instance.range))
             {
                 Target target = hit.transform.GetComponent<Target>();
                 if (target != null)
                 {
                     print(target.name + " takes " + gun.instance.damage + " damage");
                     target.TakeDamage(gun.instance.damage);
                 }
             }
 
             gun.instance.ammo--;
             if (gun.instance.ammo < gun.instance.maxAmmo / 4)
             {
                 UI.lowAmmoText.text = "Low Ammo";
             }
             else
             {
                 UI.lowAmmoText.text = "";
             }
         }
         else
         {
             UI.lowAmmoText.text = "No Ammo";
         }
     }
 }

I know it's ugly and impractical, but at this point, I'm just so desperate to get something working at the very least.

My logic here is to make an instance of a ScriptableObject named Gun, and assign that instance to whatever GameObject has this script attached.

In my scene, I have two weapons in different positions. If one player picks up one of the weapons, then it works perfectly fine - the issue comes when the other player picks up the other remaining weapon in which it is being assigned to the same weapon.

I believe the most logical thing to do is to make an array of type Gun to store all the Gun instances, as well as something that points to this being the current weapon, but the problem is that I'm not exactly sure how I would go around this.

If you want me to clarify further, then I'll be happy to do so.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

229 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

pick up ammo from specific gun? 1 Answer

Stop checking for key input 1 Answer

My automatic gun don't work help! 1 Answer

How to make my weapon be controlled by the player its on~ 0 Answers

Unity networking tutorial? 6 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges