- Home /
Simple movement causing lag (on standalone)
I have 100 AI simply moving towards player. First i used character controllers to move the AI (with SimpleMove). FPS dropped to 28. Then i removed the SimpleMove from the script and had only look rotation. FPS stayed at 60. So, problem seems to be at moving the AI.
I tried with rigidbody + capsule collider and moved the AI with transform.Translate. Again FPS dropped to 28.
So what could cause this?
EDIT: I tried with only transform.Translate. On editor i had +60 fps. When i build the game, it is like a slideshow...
Are you caching your transfroms, with 100 AI objects this could speed things up a bit if you are not.
I have not. i will look into that. Thanks!
Edit: Im not sure if did the caching right, but i did not work...
This is on Void Update()
case VisualState.InSight:
dir = target.position - myTransform.position;
if(can$$anonymous$$ove)
myTransform.Translate(Vector3.forward * Time.deltaTime * speed);
rotation = Quaternion.LookRotation(dir);
rotation.x = 0;
rotation.z = 0;
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, rotation, Time.deltaTime * rotationspeed);
break;
}
AI does have about 10 colliders with rigidbody (on $$anonymous$$inematic when alive) for the ragdoll physics.
Interesting, it works very well on on editor. On build version fps drops to 1.
Answer by toddisarockstar · Nov 16, 2014 at 09:41 PM
I believe just line 6 slowing you down. calling Quaternion.LookRotation every frame is too much. try refreshing the rotation variable every ten frames or so. and you pry wont notice a difference with slerp.
put line 6 in a timer like this:
if (timer==0){rotation = Quaternion.LookRotation(dir);}
if(timer==0){timer = 10;} timer=timer-1;
I already stated that there is no lag when only rotation is working. I have now got it working on editor, but for some weird reason build version is unplayable...
reading javascript or checking variables wont slow down a game. but asking for funtions from the game engine takes a second for the cpu to calculate and get back. normally it would make no difference but you are multiplying by 100.
some are worse than others but other examples to watch out for in your game is raycasting and asking for Vector3distance. I have a game with 50-100 objects and used timers to limit exectly how often these funtions where called. it greatly helped my project keep going!
Your answer
![](https://koobas.hobune.stream/wayback/20220613171644im_/https://answers.unity.com/themes/thub/images/avi.jpg)