- Home /
Character keeps falling through terrain (with colliders)
Hello I am an architecture student looking to use unity to create a game where I can walk through my digital models. I followed this tutorial https://www.youtube.com/watch?v=_QajrabyTJc&t=234s to make a first person controller and then I created a terrain under it with a collider. The first person controller component seems to have a collider built into it as well. However, when I run the game, the character always falls through the terrain. Shouldn't this work? I have looked through countless forum threads on this topic and haven't found any answers other than "make sure you have colliders."
I am also wondering why there are no longer any first person prefabs in unity like I've seen in some older tutorials. I feel like that would make this process a whole lot easier. I feel like I've tried everything (though I know I haven't) and nothing works! Please let me know if you have any suggestions or advice, thanks!
UPDATE: I made a new project in Unity 2018.4.20 rather than 2019.3.7 which is what I was using and it works now. Not sure why. Thanks everyone for your help!
Answer by Zymurer · Apr 03, 2020 at 08:32 PM
There may be various reasons of that, the most likely reason is that:
1-The collider of the terrain and the collider of your character(first person controller) is overlapping make sure they are not overlapping themselves.
If that dosen't work
2-You may have set the gravity so big that Unity could not calculate the colliding fast enough (by "big" I mean at least a few thousands). 3-The gravity variable may be higher than the height of your character. 4-Your teraain data may not be right. (Inspector -> Terrain Collider).
If none of them works you should do something extra. Like:
1- Assign a rigidbody (not rigidbody2D) component and make sure you havent frozen any positions, is kinematic is set to wrong and use gravity is set to true. (You can delete the part of the code that creates a gravity because you are not going to need that anymore). if problem continues 2- Assign a collider to your character. (make sure "Is trigger" is false)
if none of the solutions solve your problem please inform me about it so I can continue to help you hope that helps
The colliders of the terrain and character are not overlapping. I tried everything you mention, although I do not understand what you mean when you say the gravity variable may be higher than the height of my character. Isn't the gravity variable always a negative number (-9.81f)?
Sorry I am very new to unity and I may have misunderstood some of your answers. Here are some screenshots of how it was set up before I tried your suggestions that may help. Thank you so much for your time!! I also posted pictures of the script in response to the other comment.alt text
Yes, normally the gravity is always negative but what I meant by "value" is the absolute value of the gravity. (I just want to answer een if it solved now.)
Answer by JamesEldridge · Apr 03, 2020 at 07:33 PM
Hi, I used this tutorial as well so i'm far from an expert but will try my best to help you as i managed to get mine working, are you able to snipping tool your script and inspector of the Player so i can compare it to mine ? Thanks
Thank you!
Hi, I was looking at the pictures you posted below, in the first one i was looking at the character controller field in the inspector. It looks like you have dragged the whole model with children from the Hierarchy into the controller field. If you are on the inspector, try just clicking on the Character controller and dragging it to the Controller field from above the script, Hope this helps,
Answer by Shredimentary · Apr 29, 2021 at 10:13 AM
I had this problem, but it was for a slight variation of reason #1 that @Zymurer gave. The troublesome BoxCollider2D was not my player or terrain, but one accidentally added to 1/10 of my background game object children while testing something else entirely unrelated in the level. I have a decent number of game objects in the hierarchy so it's a newbie mistake, but hopefully someone will read this and remember to comb through the other objects in the hierarchy as well.
Answer by Nichathan · Aug 28, 2021 at 02:58 PM
None of the above answers came close to solving it for me. After a day of brain scratching, I came up this beauty that I added to my character controller update function ...it checks to see if your character has been pushed under the active Terrain and if so it applies an equivalent opposite transformation. You want to avoid adding force to the rigidbody for this because that can create a bobbing effect. Also, when logging this.transform.position.y - Terrain.activeTerrain.SampleHeight(this.transform.position)
on natural ground I was given the float value of about 1 (it was more like 1.00000000545342 and it changed as I ran around) so I gave my int variable naturalYDistance
the value of 1, since I never went below that. Then I checked, if my current position was ever less then my natural distance then I should add an opposite value to my transform.position
and I was never pushed through the ground again.
int naturalYDistance = 1;
float playerPositionCalculatedY = this.transform.position.y - Terrain.activeTerrain.SampleHeight(this.transform.position);
if (playerPositionCalculatedY < naturalYDistance)
{
float pushHeight = 1 - playerPositionCalculatedY;
transform.position += new Vector3(0, pushHeight, 0);
}
I really like your solution! Which loop did you opt to pop this into? Update, FixedUpdate, LateUpdate, a coroutine with a wait?
Your answer
![](https://koobas.hobune.stream/wayback/20220612230432im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Collider for rocks. 1 Answer
Make a simple tree 1 Answer
Large Mesh Colliders and character collider not coliding 0 Answers
Create Terrain in other Software 1 Answer