Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 ThaiCat · Jan 06, 2016 at 05:36 PM · unity 5client

UNET very high CPU usage on remote clients with projectiles spawned

I have written two classes for shooting projectiles. The first one for cannon and the second one for projectile. They are very basic and simple and also I cleaned them a bit before posting them here.

the cannon:

 public class Cannon : NetworkBehaviour
 {
     //some variables...
     ///few more methods
 
     void Update()
     {
         CheckTarget ();
     }
 
     [Client]
     void CheckTarget()
     {
         if (Input.GetAxis("Fire1") > 0f && nextShooting < Time.time) 
         {
             nextShooting = Time.time + shootInterval;
             CmdShoot(projectileLauncher.position, projectileLauncher.rotation, projectileLauncher.forward);
         }
     }
 
     [Command]
     void CmdShoot(Vector3 syncPos, Quaternion syncRot, Vector3 forward)
     {
         GameObject lastCreateProjectile = (GameObject) GameObject.Instantiate(projectileType, syncPos, syncRot);
         NetworkServer.Spawn(lastCreateProjectile);
     }
 }

the projectile:

   public class BasicProjectile : NetworkBehaviour 
     {
  //few variables
     
 
     void Start()
     {
         leftDistance = maxDistance;
         projectileSpeed = transform.forward * moveSpeed;
         GetComponent<Rigidbody>().velocity = projectileSpeed;
 
         RpcSyncMe(projectileSpeed, transform.position, transform.rotation);
     }
     
     [Server]
     void Update ()
     {
         leftDistance -= moveSpeed * Time.deltaTime;
         DestroyByDistance ();
     }
 
     [Server]
     void DestroyByDistance()
     {
         if (leftDistance <=0)
         {
             NetworkServer.Destroy(this.gameObject);
         }
     }
 
     [Server]
     void DestroyProjectile()
     {
         NetworkServer.Destroy(this. gameObject);
     }
 
     [Server]
     void OnTriggerEnter (Collider other)
     {
         if(leftDistance > 0f)
         {
             Transform block = null;// = other.transform.parent.GetComponent<MyParent> ().myParent;
 
             if (other.transform.parent != null)
             {
                 if (other.transform.parent.GetComponent<MyParent>() != null)
                 if (other.transform.parent.GetComponent<MyParent>().myParent != null)
                 {
                     block = other.transform.parent.GetComponent<MyParent> ().myParent;
                 }
             }
 
             if (block != null)
             {
                 BlockParameters bp = block.GetComponent<BlockParameters>();    
                 bp.CauseDamage(dmg, 1); 
                 DestroyProjectile();
             }
         }
     }
 
 
     [ClientRpc]
     void RpcSyncMe(Vector3 velocity, Vector3 pos, Quaternion rot)
     {
         GetComponent<Rigidbody>().velocity = velocity;
         transform.rotation = rot;
         transform.position = pos;
     }  
 
 }

I tested it for a while and found out that this code works good for host, but not so for others in terms of CPU usage. It is okay when there are 5-10 projectiles, but when projectile count increases to about 50 or more, on each connected client CPU usage becomes 100%, while on host it remains pretty low. Since 90% of projectile class code performed on server, it makes no sense to me. My first guess was it is because of many network instantiations, so i tried to check that out. I commented projectile destruction by distance, instantiated enough projectiles to make client lag, and checked if it ever goes back to normal without any further instantiations. It does not. So there is definitely something is wrong with posted network behavior classes, or i just do something wrong.

One thing to add: i was unable to sync projectile transform without that Rpc call in start(). I use Unity 5.3.1.

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 ThaiCat · Jan 06, 2016 at 06:38 PM 0
Share

i forget to add if(isServer) condition before Rpc call in Start(), but adding it does not solve the issue, there is still something wrong with the code.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ThaiCat · Jan 14, 2016 at 10:54 AM

I found the issue. Cpu load was caused by warning messages in console i did not see. So changing [Server] attribute to [ServerCallback] for some functions solved the issue by preventing logging warning messages on client.

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

40 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

Related Questions

Starting a client on standalone.exe not working but i works on editor play 2 Answers

Server not receiving message? 1 Answer

How to stop mono from preventing authentication 2 Answers

How can a client connect more players to a network game? 1 Answer

Object not updating on host 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