- Home /
Player sticks to ceiling - character controller
Hello, I'm using the character controller component for a 2D platformer. When I Jump and when my player hits the ceiling while moving it kind of sticks to the ceiling.
this is my move function:
void Move()
{
if(controller.collisionFlags == CollisionFlags.Above)
{
jumpCount = maxJumpCount;
verticalVelocity = moveDirection.y;
}
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= moveSpeed;
moveDirection = new Vector3(moveDirection.x, verticalVelocity, moveDirection.z);
moveDirection = new Vector3(moveDirection.x, (moveDirection.y - gravity * Time.deltaTime), moveDirection.z);
if(controller.isGrounded && moveDirection.y < -1)
moveDirection = new Vector3(moveDirection.x, (-1), moveDirection.z);
controller.Move(moveDirection * Time.deltaTime);
}
verticalVelocity is set upon pressing the jump button.
When the player jumps and hits its head it comes down immediately, it only happens while moving.
EDIT: The moveDirection is set to Vector3.zero at the start of the update. When I print the vector before setting the verticalVelocity, the y-value is always 0, even when I move while hitting the ceiling. The character is just not coming down.
Do you know why the character is sticking? For example if the cause is penetration into the ceiling, you might be able to solve the issue by turing up the solver iteration count(Edit/Project Settings/Physics), limiting the character velocity, or detecting the situation and moving the character.
Answer by AxionJaxon · Jan 15, 2013 at 06:27 PM
Your problem is with the character controller. Set the "Step Offset" option to zero in order to make your character no longer stick. If you still want the character to work with small steps, I suggest hacking it with transparent textures or trying different smaller values in Step Offset, whatever works for you.
wow yes that works :o Will it bring problems to the rest of the movement?
Well I'll just mark your answer as correct and make a note of this..
Sorry, it shouldn't affect the character's movement in any way other than your ability to go up steps. If you have cubes (or some other jagged geometry) the character controller can look for a "step" (like stair step) up to some amount higher than they are on now. This fix reduces the size of those steps to zero, so you can no longer do that, but if you already weren't then there should be no issue.
Yeah, what Axion says is correct - Step Offset has no effect on this ceiling-stick problem.
This is nearing 8 years and still managed to help me out. I think it's important for me to say that this fixed an issue I had which revolved around newton's second law- I was using a character controller and for each collision the player received force back, and for whatever reason, was sticking to the underside of slopes. So clearly there are a lot of problems with the step offset.
Moral of the story is, this issue is old, and hasn't been fixed, which tells me you should defenitely make your own step offset or your own character controller.
Answer by Loius · Jan 11, 2013 at 06:04 PM
verticalVelocity = moveDirection.y;
"If the player hits their head, move the player at their current velocity"?
Don't you want to set that to zero instead (or set MD.y to zero first)?
Hi, I changed the script since I posted this question, but the problem and the part you addressed is still there.
I set the movdirection to 0,0,0 at the start of the update. When I print the movedirection just before setting the verticalVelocity it does say that the vector is (0,0,0).
So basically yes, I do set the Y to zero.
It ONLY happens when moving though. If I just jump and hit something, the character comes down immediately.
When I print the vector before setting the verticalVelocity, the y-value is always 0, even when I move while hitting the ceiling. The character is just not co$$anonymous$$g down.