Hello, i have one weird problem on Unity. I’m using the character controller on my player and i created state machines hierarchy to handle the player properties like Run Jump Walk Etc… when the jump is performed it works good like it should be. The problem is when the player touch the ground and the jump state should exit and change into grounded state. From the jump state class the property isGrounded of the player character controller result to be false but if i debug the same property from the player class it is true. This is really weird! Has anyone ever experienced a similar problem? Or have an idea of what the problem could be? All the state classes has the player class reference obviouslly. This is where i check isGrounded from the jump class:
public override void CheckSwitchStates() {
if (_context.CharacterController.isGrounded) {
SwitchState(_factory.Grounded());
}
}
It call SwitchState that quit this state and change the current state to grounded state.
The only failure I know of for the grounded property in CC comes from calling the .Move() method more than once per frame.
Are you doing that? If so, stop doing that. Here’s how:
CharacterController CharMover broken:
I wrote about this before: the Unity example code in the API no longer jumps reliably.
If you call .Move() twice in one single frame, the grounded check may fail.
I reported it to Unity via their docs feedback in October 2020. Apparently it is still broken:
Here is a work-around:
I recommend you also go to that same documentation page and ALSO report that the code is broken.
When you report it, you are welcome to link the above workaround. One day the docs might get fixed.
Otherwise, sounds like you have a state machine bug. Here’s how to debug it:
You must find a way to get the information you need in order to reason about what the problem is.
Once you understand what the problem is, you may begin to reason about a solution to the problem.
What is often happening in these cases is one of the following:
the code you think is executing is not actually executing at all
the code is executing far EARLIER or LATER than you think
the code is executing far LESS OFTEN than you think
the code is executing far MORE OFTEN than you think
the code is executing on another GameObject than you think it is
you’re getting an error or warning and you haven’t noticed it in the console window
To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.
Doing this should help you answer these types of questions:
is this code even running? which parts are running? how often does it run? what order does it run in?
what are the values of the variables involved? Are they initialized? Are the values reasonable?
are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)
Knowing this information will help you reason about the behavior you are seeing.
You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as Debug.Log("Problem!",this);
If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.
You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.
You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.
You could also just display various important quantities in UI Text elements to watch them change as you play the game.
If you are working in VR, it might be useful to make your on onscreen log output, or integrate one from the asset store, so you can see what is happening as you operate your software.
Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.
Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:
When in doubt, print it out!™
Note: the print() function is an alias for Debug.Log() provided by the MonoBehaviour class.
Is what i’m doing, it’s really weird because it have both true and false value at the same time. I also tried to do Debug.Log(characterController.IsGrounded) from both script and running at the same time. One value is true one is false. This thing is completely senseless to me.It looks like a Unity bug, i don’t know.
CharacterController.isGrounded requires a constant downwards force for it to function properly. While the docs state: “Was the CharacterController touching the ground during the last move?” it’s really asking “Was the character controller intersecting with the ground before we adjust the capsule out of it during the last move?”.
So if you remove your downwards force when in a ‘grounded’ state, this stops working.
Basically, like in real life, there always needs to be gravity.