This code makes the game slow, I have tested by commenting it out and just these few lines, over many characters all using this simultaneously, it slows the game considerably.
The script simply checks for the next node, moves the character towards this node, changes if it gets to the node, and check to see if its near the player.
if(listGenerated == true) //enemy move script
{
//look at current waypoint
transform.LookAt(route[xl].transform.position);
//translate
transform.Translate(Vector3.forward * speed * Time.deltaTime);
//change to next
if((transform.position - route[xl].transform.position).magnitude < 1)
{
xl++;
}
//go to player if near
if((transform.position - finish.transform.position).magnitude < 1)
{
Debug.Log("Reaches the players position!");
}
}
}
I am no optimization guru, but if I am not mistaken, Vector3.Distance would be much faster than calculating the Vector’s magnitude to determine how close an object is to it’s destination.
Good advice which gives a quite small performance increase.
Theres more optimising needed though.
Does the parsing go through code which is not in a loop which is being used, that is, if the first part of a script only runs once, yet is very long, does its presence slow the script down at all?
Vector3.Distance is no faster at all; it’s just a convenience function for “(a-b).magnitude” (which is noted in that doc page). Using sqrMagnitude would be faster than magnitude, since there is no square root in that case.
Caching the transforms would gain some speed. Also, Debug.Log is very slow, if you’re actually using that in the production code.
sqr magnitude was not much faster although it is slightly
The debug log is slow however its just holding the place and only gets called when that condition is true which isnt where the problem is. I did try taking the line out anyway with no success.
There are several characters and it seems to be fast running 2 or 3 however more slow it down.
There is a bulk of code, which is outside of the loop, yet in the same script(redundant script at that point). It should not be parsing through it however I wondered if a larger script slows it down regardless?
It sounds like you only have a handful of characters (a dozen or so?). If so, that script on its own I doubt would have such a performance impact. Can you post the rest of the update script?
Indeed, it is very hard to believe this piece of code is a performance bottleneck unless it’s called a huge amount of times per frame. The surrounding code may give us some more information.
I was guessing you were doing crowd simulation or something similar with 100s, if not 1,000s or more copies of that running and looping over it a lot of times in each one.
As the others have said, there is nothing in the section of script you have there that would bottleneck things unless it’s being looped over a lot (and I mean a lot of a lot)
Debug log in this doesnt doesnt affect it at all read earlier post.
I am now only calling the script once every x number of frames, since the monsters only move so fast, it doesnt need to run this hundreds of times per second, and I’ll probably do this per second.
This has increased my frame rate tremendously although I have to ask, is there not a better way to implement calling things only every often?
That code doesn’t take long enough to execute to matter that you were calling it every frame. Optimisation is great and all, but your fundamental problem isn’t the amount of time it takes for that block of code on its own to execute.
If I loop that block of code 10,000 times it takes about 12ms in total to execute all 10,000 runs through. If your only running over it a few dozen times per update we’re taking about a fraction of a ms per frame.
I did read the posts and the Debug.Log hadnt been mentioned then i had to run out after posting and came back to find it had failed and was offering to resend the information so i did thus my post was late
:evil:
This one is interesting, and once again apologies to those I have offended in my stress.
Basically the script had a lot of variable initialisation and also a lot of code, which although this only runs once, its very presence seemed to slow the rest of the code significantly.
It was as if my earlier theory about ‘out of loop’ code being parsed is actually true.
RMan - to answer your questions about ‘out of the loop’ code, we have to make sure that you are using the correct terminology so that we can be sure we are talking about the same thing
The function ‘Update’ is executed many times per second (perhaps 100). Anything within this function is run every time that the function is called, whether it is inside a loop or not.
Example:
function Update(){
Debug.Log("This code takes a long time (500ms)");
for (var i=0; i<1000; i++){
Debug.Log("This takes shorter, but is in a loop (0.01ms)");
}
}
Every time that the update function is called (which is on every frame), the code above would take 500 + (1000*0.01) = 510ms to execute, meaning that your entire game would slow to around 2 fps!
So the basic answer to the above is YES - anything outside the loop (but still inside Update) will slow your code down.
However, a second example using the function ‘Start’, which is only called once when the game first starts:
function Start(){
Debug.Log("This takes a long time (500ms)");
}
function Update(){
for (var i=0; i<1000; i++){
Debug.Log("This takes shorter, but is in a loop (0.01ms)");
}
}
In this second example, the code that takes a long time to run would maybe cause a slight delay just as your scene first appears, however once the game is running, each frame should only take 1000*0.01 = 10ms to execute, meaning your game would run at 100 fps (with no other factors such as polycount etc).
So the basic answer to the above is NO - anything outside the loop, but also outside of Update shouldn’t slow your frame rate.