Character Controller not resetting flags correctly?

I was going to report this as a bug, but i want to make sure that i’m not doing anything stupid first.

I have a very simple scene with a character collider and a movement script.

private var controller : CharacterController;
var velocity : Vector3;
private var playerGravity : Vector3 = Vector3(0,-30,0);
private var jumpVel : float = 10.0;

function Start()
{
	controller = GetComponent(CharacterController);
}
var flags : CollisionFlags = 0;;
var timeColliding : float = 0;
function Update () {

	// move right 1m a second;
    velocity.x += 1 * Time.deltaTime;
    
    // apply player gravity
	velocity += playerGravity * Time.deltaTime;
	
	// move the controller
	flags = controller.Move(velocity * Time.deltaTime);
	
	// Respond to any collision we might have had
	if((flags  CollisionFlags.CollidedBelow) != 0)
	{
		velocity.y = jumpVel;//0;
		
		// just making sure the problem is only for hitting your head
		timeColliding = 0;
		
	}
	
	if ((flags  CollisionFlags.CollidedAbove) != 0)
	{
        velocity.y = 0; //-jumpVel;
        
        // increment collide timer
        timeColliding += Time.deltaTime;
    }
}

The script is set to make the controller to bounce up when it hits the ground, and then lose velocity and fall normally when it hits the ceiling.

The bounce on the ground part works fine however, on colliding above, the controller moves very slowly down for over a second and then starts falling normally.

What appears to be happening is that CollidedAbove flag seems to be set for a long time after the controller itself has moved away from the object it hit. ( i’ve set a variable called TimeColliding so you can see whats happening)

As a test I tried mirroring gravity and flipping the jumpVel values. The Behaviour was as I expected, which further points to the CollidedAbove flag.

As an additional note, this only happens when the velocity.x is greater than 0;

I have attached a package file to this post.

Any help appreciated.

354554–12330–$controllerproblem_209.unitypackage (5.87 KB)

I recall observing the behavior you describe when trying to tweak the FPS controller so that the controller would ‘bounce’ off of ceilings instead of ‘sticking’ to them. I got the same results that you describe here, and ended up working around it by zeroing the y velocity only when a collision from above was detected and the y velocity was already greater than zero.

As for the behavior itself, I imagine it can be traced back to how PhysX handles things internally, but I don’t know why it behaves that way exactly, or if it’s something that could be changed or fixed.

Thanks for the tip Jesse, i’ll give it a try and post back if it works. :smile:

I’m not convinced this is a PhysX thing, mainly because setting gravity to go upwards and then mirroring the jumpvels (so in essence you are jumping off the ceiling and banging your head on the floor) does not cause this problem to happen.

So I’m guessing its something to do with the CharacterController.Move() function.

Still worth putting a bug in?

Your suggestion worked perfectly, thanks Jesse!