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 /
  • Help Room /
avatar image
0
Question by JoaquinSosa · Sep 15, 2020 at 07:59 PM · networkingmirror

Remote clients can't spawn objects - Unity mirror networking

I'm trying to make an online third person shooter, so far I managed to get it working, but for some reason I can't spawn bullets from any remote client but I can from the host. It seems that either the command is not being called or that it just won't spawn. I have a lobby system, so when all player are ready it just changes the scene and spawn the corresponding PlayerObject with authority, all shooting mechanic scripts are in this GameObject. I won't include the lobby code since I think it won't affect anything, if you need it feel free to ask. Client authority is ticket.


The issue: It will only spawn if the one shooting is the host and everyone will see it properly, but if the one who shoots is a remote client it won't spawn for anybody, not even for the remove client itself. It's like if I couldn't send CMDs from remote clients.


Update: I tried calling Instantiate() locally and then using the CMD to say NetworkServer.Spawn(), and it worked perfectly for the host (just like before), but it didn't for the remote clients, they see the local Instance but the CMD doesn't seem to spawn anything in the server.


Here's the code:

This method is called every frame you fire from LateUpdate, fireRate is handeled previously

 private void FireBullet()
 {
     if (ammoCount <= 0) return;

     ammoCount--;
     foreach (var particle in muzzleFlash)
     {
         particle.Emit(1);
     }

     activeWeapon.SpawnBullet(bulletPrefab, bulletOrigin, bulletSpeed);
 }

This is executed inside the player

 private Transform bulletOrigin;
 private GameObject bulletPrefab;

 public void SpawnBullet(GameObject bulletPrefab, Transform bulletOrigin, float speed)
 {
     this.bulletOrigin = bulletOrigin;
     this.bulletPrefab = bulletPrefab;
     Debug.Log("Spawning");
     CmdCreateBullet(speed);
 }

 [Command]
 void CmdCreateBullet(float speed)
 {
     GameObject bullet = Instantiate(bulletPrefab, bulletOrigin.position, bulletOrigin.rotation);
     bullet.transform.LookAt(lookAtTransform);
     bullet.GetComponent<Rigidbody>().AddForce(bullet.transform.forward * speed, ForceMode.Impulse);
     NetworkServer.Spawn(bullet, connectionToClient);
 }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by JoaquinSosa · Nov 09, 2020 at 05:53 PM

The issue was that I was trying to use variables in the server that I had only defined locally, there's no error message for this but it won't work properly. What I ended up doing was first creating an enum with the types of bullets so I don't have to pass the prefab as a parameter, if it's a prefab and we are using C# you can cast a byte (or int) into an enum (you're sending way less information to get the same result), and since you're spawning a bullet everytime you shoot is an excellent practice, every time I shoot the server gets a call (CMD), in the cmd I have the bullet type, bullet origin, bullet direction and force, since all that information is passed as parameters the server doesn't use any local variable. In the CMD I use said information to spawn the bullet in the server since we only want to detect server-sided bullet collisions but have a visual of it in every client, that's when RPC comes into the game. After spawning the bullet in the server I call a RPC which will spawn the bullet in every client except from the host, since the host is the server, the bullet has already been spawned, the bullet spawned in every client is only used as a visual, not an actual bullet which has collisions, this is due to the delay between players, one player could see a collision while someother doesn't. Managing collisions in the server is the best practice; to only detect collisions from the bullet spawned in the server and ignore the ones spawned in the clients I use NetworkServer.isActive, which will return true if that object is active only in the server, after overwriting OnCollisionEnter I check if NetworkServer.isActive, if true then proceed with the collision management, otherwise just return. When hitting players and taking damage is easy since you always return if you are not the server all the things you do when a bullet hits something is done in the server, that's why you can call damage methods which are supposed to be server sided. This is the whole explanation of how I ended up addressing the shooting mechanism with Mirror, the base solution to the question above is just about variable managment, where I was trying to access data from the server without it being defined in it. In case anyone needs some code example just message me.

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

257 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 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

I want to switch scenes in a lan-coop-game... 0 Answers

Multiple Network Managers detected in the scene + Destroying assets is not permitted to avoid data Loss. 0 Answers

Mirror Networking - Network Discovery not working on LAN 3 Answers

Bad collision detection in client's view when using Networking (Mirror) 0 Answers

Mirror uses the same input for every client 0 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