Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by Mint92 · Apr 16, 2014 at 12:12 PM · networklagbulletrpcclient

Network Instaniate,Lag,Bullet Prefab

Hey guys, when i play the game as the server, the bullet fires like it should without any problems, however when i fire the bullet on the client side, nothing shows up and when click back onto the server, you can just see the bullet firing. I dont understand why its not updating properly on the client side.

 void Update () 
     {
         if (game.running && Network.player == owner) 
         {
             playerActive = true;
             
             bool shootButton;
 
     
             if (Input.GetKey("space")) 
             {
                 shootButton = true;
             }
             else 
             {
                 shootButton = false;
             }
         
             if (shootButton == true && Time.time > nextFire) 
             {
                 Debug.Log("Firing!");
                 nextFire = Time.time + reloadTime;
                 
                 Transform turret = transform.Find("TurretPivotControl");
                 TurretControl turretScript = (TurretControl)turret.GetComponent("TurretControl");
                 
                 
                 if (Network.isServer)
                 {
                     Debug.Log("Server is spawning Bullet" + bulletCount + " fired by " + gameObject.name);
                     
                     // call FireBullet RPC in clients
                     networkView.RPC("FireBullet", RPCMode.Others, gameObject.name, "Bullet"+bulletCount, transform.Find("TurretPivotControl/Turret/BulletSpawnPoint").transform.position, turret.transform.rotation, turretScript.getForce());
 
                     // create bullet on local server                    
                     GameObject bullet = (GameObject)Instantiate(bulletPrefab, transform.Find("TurretPivotControl/Turret/BulletSpawnPoint").transform.position, turret.transform.rotation);
                     bullet.name = "Bullet"+bulletCount;
                     
                       ((Bullet)bullet.GetComponent("Bullet")).setFiredBy(gameObject.name);
                     
                     // add bullet to server list of bullets
                     game.addBullet(bullet);
                     
                     bulletCount++;
                     
                     bullet.rigidbody.AddForce(turret.right * turretScript.getForce());
                 }
                 else
                 {
                     networkView.RPC("Log", RPCMode.Server, "Client has fired Bullet"+bulletCount);
 
                     // call server FireBullet RPC 
                     networkView.RPC("ServerFireBullet", RPCMode.Server, gameObject.name, transform.Find("TurretPivotControl/Turret/BulletSpawnPoint").transform.position, turret.transform.rotation, turretScript.getForce());
                 }
             }
         }        
     }

I then call it through the RPC

 [RPC]
     public void ServerFireBullet(string shipName, Vector3 position, Quaternion rotation, float force)
     {
         if (Network.isServer)
         {
             Debug.Log("Server ordered to spawn Bullet" + bulletCount + " fired by ship " + shipName);
 
             // tell other clients that the bullet was fired
             networkView.RPC("FireBullet", RPCMode.Others, shipName, "Bullet"+bulletCount, position, rotation, force);        
             
             // find the local server version of the ship
             GameObject ship = GameObject.Find(shipName);
 
             Transform turret = ship.transform.Find("TurretPivotControl");
             
             // create local server version of bullet
             GameObject bullet = (GameObject)Instantiate(bulletPrefab, ship.transform.position, bulletPrefab.transform.rotation);
             bullet.name = "Bullet"+bulletCount;
 
             ((Bullet)bullet.GetComponent("Bullet")).setFiredBy(shipName);
             
             Game game = (Game)GameObject.Find("GameManager").GetComponent("Game");
             
             // add bullet to server list of bullets
             game.addBullet(bullet);
             
             bulletCount++;
             
             bullet.rigidbody.AddForce(turret.right * force);
         }
     }
     
 
 
 
     [RPC]
     public void FireBullet(string shipName, string bulletName,  Vector3 position, Quaternion rotation, float force)
     {
         networkView.RPC("Log", RPCMode.Server, "Client ordered to spawn " + bulletName + " fired by ship " + shipName);
         
         // find local client version of ship
         GameObject ship = GameObject.Find(shipName);
         
         Transform turret = ship.transform.Find("TurretPivotControl");
         
         networkView.RPC("Log", RPCMode.Server, bulletName + " fired by " + ship.name + " at " + ship.transform.position);
         
         // create local client version of bullet
         GameObject bullet = (GameObject)Instantiate(bulletPrefab, ship.transform.position, bulletPrefab.transform.rotation);
         bullet.name = bulletName;
         
         ((Bullet)bullet.GetComponent("Bullet")).setFiredBy(shipName);
         
         bullet.rigidbody.AddForce(turret.right * force);
     }    
Comment
Add comment · Show 12
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 Benproductions1 · Apr 16, 2014 at 12:24 PM 0
Share

Since FireBullet only gets called on the server, what would be the point in sending an RPC with Log to oneself, ins$$anonymous$$d of just using Debug.Log?

You should also cache your finds/component gets. Not only will it make your code much much faster, it will also make it much cleaner and easier to read.

avatar image Mint92 · Apr 16, 2014 at 01:27 PM 0
Share

well im using unity 2.5, so some of the code is probably outdated

avatar image Benproductions1 · Apr 16, 2014 at 01:49 PM 0
Share

The unity version had nothing to do with my suggestions, using an outdated version does not excuse writing bad code.

avatar image Mint92 · Apr 16, 2014 at 04:35 PM 0
Share

sorry, but most of the code that i was given was done by my tutor. im just using his framework to make my game work

avatar image Benproductions1 · Apr 17, 2014 at 09:00 AM 0
Share

The code looks like it should work. Have you tried debugging it yourself? Does it throw any errors?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Mint92 · Apr 27, 2014 at 12:08 PM

Okay so i was doing abit of research, and i didnt have "Run in Background" check under the player settings. Now that the bullet seems to spawn from the client side, and i can see, it gets deleted very quickly.

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

21 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

Related Questions

RPC Call Mix Up Issues 0 Answers

Get RPC senders ID, or IP? 1 Answer

Network RPC - exclude specific client 1 Answer

client to client ping - NAT punchthrough 1 Answer

Spawning Clients 1 Answer


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