- Home /
Very Low fps.
Hi there!
I have script wich realizes creatures behaviour. If there are 3 or more monsters with this scripts FPS will become very low. If i delete all Update methods (Update(), FixedUpdate(), LateUpdate()) fps rises. So can u advice me please how to rewrite my code to archieve normal fps in my game.
void Update()
{
if (gameObject.transform.rotation != spawnRotation)
{
//this.gameObject.transform.rotation = Quaternion.AngleAxis(rotOffset, Vector3.up);
}
if (networkView.isMine)
{
if (target != null && isAlive && startAggro && target.GetComponent<Player>().isAlive)
{
Attack(target);
}
}
Die();
curHP = Mathf.Clamp(curHP, 0, maxHP);
if (isAlive)
{
if (checkForReAggro && attackersList.Count > 1)
{
StartCoroutine(AggroDontHitRecovery(attackersList.Last()));
if (toggleToOtherTarget)
{
AdjustTarget(attackersList.Last());
toggleToOtherTarget = false;
}
}
if (_bUpdateRestoreHpAfterRunaway)
{
StartCoroutine(WaitAfterRunAwayForRestore());
_bUpdateRestoreHpAfterRunaway = false;
}
else
{
_bUpdateRestoreHpAfterRunaway = false;
}
}
}
void LateUpdate()
{
if (_allowWaypointMove && isAlive && GameObject.Find("MainCamera").GetComponent<Server>().connected) //!It used to be in FixedUpdate 11.01.15 / 0:12:00
{
WaypointMovement();
}
}
void FixedUpdate()
{
if (isAlive)
{
Aggro();
}
if (networkView.isMine)
{
networkView.RPC("AdjustThePos", RPCMode.AllBuffered, this.transform.position);
networkView.RPC("AdjustTheRot", RPCMode.AllBuffered, this.transform.rotation);
}
if (GameObject.Find("MainCamera").GetComponent<Server>().connected && this.gameObject != null)
{
if (isAlive)
{
networkView.RPC("AdjustSpeed", RPCMode.All, (transform.position - lastPos).magnitude);
networkView.RPC("SendFloat", RPCMode.All, "Speed", speed);
}
}
if (target != null)
{
if (!(target.GetComponent<Player>().isAlive))
{
networkView.RPC("KilledTarget", RPCMode.All);
}
}
}
Perform following change at line 68:
Replace
GameObject.Find("$$anonymous$$ainCamera")
with
Camera.main
Linear search is always a bad idea when performed frequently.
If i use assigning via the inspector, wont it be faster? 'cause i have a few main cameras
The Find
method is very slow when there are a lot of objects in the scene and calling a camera directly will be much faster and use up less memory
Your answer
Follow this Question
Related Questions
Android update math at higher hz? 2 Answers
How to handle fps for game? 1 Answer
Script lowering frame rate 3 Answers
How to create a dynamic map in hand of FPS like Far Cry 2 or Firewatch? 0 Answers