How to reconfigure AIPath.cs to use a Rigidbody instead of a CharacterController?

My game involves a very high number of units pathfinding simultaneously. Currently, my biggest performance-hitter is CharacterController.Move(), being called from AIPath.cs. I’d like to update my project so that I’m using Rigidbodies instead of CharacterControllers.

I tried replacing my CharacterControllers with Rigidbodies, but my characters just started sliding around everywhere at warpspeed.

I wanted to eliminate the possibility that my own code was screwing things up, so I created a brand-new Unity project with nothing existing in it, imported the A* Pathfinding Project, opened up the Example10 scene, and tried to modify the “Bot” so that it used a Rigidbody instead of a CharacterController. Unfortunately, the results were no good; I had to turn the speed way up just to get it to move, and once it started moving, it was sliding around like it was on ice.

I did a search for the issue on this forum, and found several individuals who ran into this exact issue. I followed the advice I found in those threads; I tried changing “Update” to “FixedUpdate”, I tried using rigidbody.velocity instead of rigidbody.AddForce, and tried everything else that was recommended, but I still couldn’t get the Bot to perform movement that resembled the movement from a CharacterController, even when working with a completely fresh Unity project.

Can you please walk me through the steps I would need to follow to get a seeker to use a Rigidbody instead of a CharacterController, and have identical behavior to a CharacterController?

(It’s probably worth mentioning that I’m using the most recent build that works on Unity 4, because upgrading to Unity 5 is not an option for me right now.)

Thanks for your time!

2 Likes

Hi

Yeah, It has been on my todo list for a while to make sure that the script works more similarly to the character controller when just switching out those components.

I think instead of

 rigid.AddForce (dir);

you should use something like

 rigid.velocity = dir;

(did you try this? did it not work well?) Possibly you will want to add some gravity as well.

dir.y -= 9.81f * Time.deltaTime;
rigid.velocity = dir;

This worked great for the bot in the pathfinding test scene; its movement perfectly mimicked the movement of a CharacterController! Unfortunately, the exact same code isn’t working out well for my game’s characters:

I feel like the problem is probably a typo somewhere in my code, but I’ve spent a long time trying to troubleshoot the issue, and I can’t locate the culprit of the problem.

Do you have a time estimate for when you’ll be able to revise the AIPath.cs script and make Rigidbody behavior identical to CharacterController behavior?

I’m not sure why it would work so differently. Are you using the exact same AIPath script? If so, try changing the parameters one by one until you find what is causing it.

Yes, exact same AIPath script. Changing parameters one-by-one until I find the problem is my usual method of solving issues like this, but in this particular case, it’s not working properly under any configuration except the original configuration, which made use of the CharacterController only.

I have a separate, but very closely related question. Let’s say that I decide to forgo rigidbodies and colliders altogether and just use transform.Translate instead, to save myself a headache and improve performance even further. The only downside is that collisions are not detected; I can actually live with that, but there is one problem - stairs. The code needs to be re-written so that characters move upwards / downards in space towards the height of the next waypoint on the grid graph, but I’m not having any success pulling that off.

How would I modify the code that determines “dir” so that instead of just translating “forward” towards the next waypoint, I am translating to the height of the next waypoint?

I would recommend that you use a simple Physics.Raycast call every frame to figure out where the ground is. That is cheaper than using a character controller/rigidbody but it solves the issues with the terrain height.

I think I might have found the solution to the problem this thread was discussing. The major change I made was I made to the code was changing the Update function to FixedUpdate Like so.

As you can also see in the image I returned the movement of the rigidbody to AddForce since from prior experience I had a hunch it wasn’t the issue and it worked. I would have posted my rigidbody settings too but I can only post one image per post I think with this level of account.

1 Like

That’s very helpful! I’m grateful to you for helping me out!

I’d honestly love to see your rigidbody settings, too.

Ok sure here they are.

Having some drag makes sure you don’t have that slingshot effect.

1 Like