Why my framerate fall down to 5-6FPS once I use CharacterController.Move()

I need to make several enemies (AI) move.

Once I use CharacterController.Move() or CharacterController.SimpleMove(), the framerate would gradually fall down to 5-6FPS (SimpleMove() would be a little better, but still not acceptable).

But if I simply use Transform.Translate(), the enemies would not be able to climb/descend along the terrain. They would rather penetrate into the terrain.

So anyone could help me find out a way out of this issue?

6 Answers

6

There must be something wrong - the CharacterControllers would slow down Unity this way only if you had an entire enemy army! I bet on some coroutine being continuously called in your AI - this creates an always increasing number of coroutines running in parallel, which gradually slows down your game.

Post your AI script - only analyzing it we can help you to find the culprit.

I don't believe 25 enemies could slow down your game this way (unless it's a mobile device game - is it?) You must use FixedUpdate to do rigidbody things (to add a continuos force, for instance), since the physics engine syncs with FixedUpdate. But the CharacterController doesn't use the physics engine, so it's better to keep it in Update. I really suspect that some out-of-control yield or StartCoroutine is the responsible for your problem.

–

That's what I think too. This often is caused by coroutines being called at each Update - they are like the butler in these cases: always the first suspect. @justbecrazy, post your script, or you will just be driven crazy!

–

Just an overview here:

myTransform.animation.Play("run");

You are doing this every update. Is the animation shorter than an update cycle? Is the 'run' cycle really necessary this quickly?

If the animation is several frames then you want to call this at a slower rate.

Hope I called this correctly.

put a Debug.Log() in each if and else statement. The maniac activity should show up in the console. If it doesn't then you got bigger problems than Answers.unity.

I finally got it!!!After one whole day work,at last!!!

It’s all about the setting of my CharacterController. I used to set it too large in Height,Radius,…all other parameters, because my model imported is big.

And then I changed all of these stuff back into the values suggested in the official Reference page. And the FPS now is 80+ for 10 enemies!

Oh yeah!!

Holly crap, you're right! I had a small model that I scaled. I set the Character Controller radius to 0.5 so it matches the sizes of a model and got 50 fps drop on SimpleMove() call. Now when I set radius to 0.15 I get 70 fps again. Unity should address this issue IMHO.

–

Had you Scaled those Enemies programatically or You have Created a new Prefab's of Enemy with required sizes. I had Scaled those objects in my Prefabs only and saved them as new. For FPS should i care in these case of scaling ?

–

+1 for changing the radius of the capsule. I only have one enemy in the scene, and the CharacterController only gets called if the Player is too close. The only things in the function are transform.LookAt (player) and SimpleMove (direction*Speed).
Changing the radius of the CharacterController from 0.02 to 0.01 solves my slowdown issue (that only happens when you get too close to the NPC, and the chase function is called)
HTH
Ian

I had a similar problem where Move and SimpleMove were taking a lot of CPU. I finally tracked it down to setting transform.localScale every frame (usually to the same value), which I’m guessing did something to make the CharacterController do a lot of work.