- Home /
Character controller and Terrain
We are developing 2D endless running type game and we are facing problem with character controller and terrain mesh collider. The player falls through ground.
If terrain is flat or a player goes down, the player doesn't fall through ground. Problem comes when the player goes up. I attached screen shot of game play that player goes through terrain collider.
I'm using character controller Move script from this reference.
Does anyone has solution for this problem? Thanks in advance.
I'm not entirely familiar with C#, I use Javascript, but could you try to add a function in
' if (controller.isGrounded){ '
rigidbody.useGravity = false;
and turn gravity back on if 'isGrounded' is false?
Just an idea.
I've looked at this a few times and I keep looking at your use of $$anonymous$$ove & hard reset of direction = 0. I suspect you are passing a direction delta into $$anonymous$$ove that's moving your ccontroller below the terrain. I don't know why I think that, just a guess.
Still. The isGrounded will have some sort of is/isn't setting
so something like
if(!controller.isGrounded) -enable the gravity function
or
if(controller.isGrounded) Gravity.enabled = false;
I'm not sure, I'm just throwing ideas out there.
For grins you might try modifying this to Simple$$anonymous$$ove and adjust your speed accordingly.
Answer by saruul34 · Jan 28, 2014 at 09:39 AM
I have figured out why my character falls down. As getyour411 suggested that hard reset, I have read and played with moveDirection.
Main thing is character controller checks collision while it moves. But my character is only does jump so it never detects that collision. So I added little movement right and left side. Now it works what I expected.
private float x_Axis = 0.01f; // Very little value that moves right and left.
.....
....
void Update(){
....
if (controller.isGrounded) {
//Switching right and left
x_Axis = -1 * x_Axis;
//That line i have changed
moveDirection = new Vector3(x_Axis, 0, 0);
if (Input.GetButton("Jump"))
moveDirection.y = jumpSpeed;
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613131901im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
2D Efficient Realtime Terrain Generation 0 Answers
2D character controller getting stuck on walls 5 Answers
Hi can I use DOTween as my 2D charatercontroller? 1 Answer
Freezing on destruction of terrain. 1 Answer
Help with collisions with terrain 0 Answers