- Home /
Best way to simulate Character Controller under Fixed Update?
Hi, my question is what is the best way to simulate a Character Controller, which is based on Update and Time.deltaTime, under FixedUpdate?
I tend to achieve the same effects of Character Controller that is able to move base on speed cmd, has simple collision exclusion with others but with no physics simulation of the collision that mess up the rotation and position after collision, able to stand and move along curve 3d ground (stand normal to its current ground position).
I have tried rigidbody with colliders, use set velocity to move, but but a single collision with other object would mess up the movement. If I lock the X,Y,Z rotation and position, then I am not able to make it move along the curve of the ground.
Another approach I am now considering is applying a extremely long collide box along y axis for each unit, which guarantee an intersection with the ground. Then, at each FixedUpdate, check the intersection position and normal with the ground, then adjust the stand stance of the unit and make the movement based on the normal. But this sounds quite low efficiency, as it needs to check for the collision on each FixedUpdate(physical update) loop.
Is there any better way to achieve the Character Controller based on FixedUpdate? Or is there any source code or underneath mechanism of the Character Controller I can reference to? Many thanks in advance for any help
I am asking such a question because I am aiming to develop a 3D RTS game that supports multiplayers, and tend to use lock-step technique to achieve that (1500 Archer Technique). Thus I can not rely on Character Controller for units as it is based on Updates, which means its movement may varies based on frame rates.
Answer by Owen-Reynolds · Jun 15, 2012 at 02:53 PM
You should be able to reuse any charController script almost exactly as-is -- change Update
to FixedUpdate
and fix the minor problems:
o Replace Time.deltaTime
with Time.fixedDeltaTime
.
o Anything that samples 1-time input will need to be moved to an Update (FixedUpdate can miss seeing them.) For example: global bool jumpNow;
, set true in Update if jump key pressed. Checked at usual place in FixedUpdate and reset to false afterwards.
o I believe Vert/Horizontal Axis is fine to check in FixedUpdate. Unity is maintaining the values for you. You may need to check the scroll-wheel in Update (it may count each scroll-tick as a discrete event.)
Thanks for the reply :)
The CharacterController is a binary component rather than a modifiable script if I get it right, how can I modify it?
Ins$$anonymous$$d of fixedDeltaTime, I tend to use some constants, so the movement is actually based on the number of loops rather than the actual time passed. This is required by the lock-step technique.
Input will be handled in Updates, yes, thanks for the re$$anonymous$$der :)
The problem with rigidbody and Vert/Horizontal Axis is that, objects keep bouncing with each other when they collide, which I don't want. But if I freeze the rotation and position of the rigidbody, then I get no access to 'set velocities' and make the rigidbody moves along the ground. Is there a way to cancel the bouncing, and make rigidbody movement applicable at the same time?
The interesting part of a CharContr is the built-in $$anonymous$$ove
function, which simulates a "physics move" (and isn't linked to Update.) There are a number of scripts (some in Unity Standard Assets) which "run" a character controller. They are the things that generally run in Update, but could run in Fixed.
Yes, rigidbodies bounce around. The CharCont+script approach is specifically meant to not do that.
Ok, Will go and have a look on those. Thanks for the answer again :)