- Home /
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.
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.
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.