- Home /
Player sometimes, only occasionally falls through terrain
Edit Hi again: so, not sure this is going to be a solution to my issue of occasionally falling through the terrain (read below) but am wondering if adding a capsule collider to my player might help. I recently had the same issue with an NPC falling through the terrain when a rigidbody with gravity was applied, and placing a capsule collider on them stopped that. I'm gonna go ahead and try doing that, but thought I'd leave this edit here in case anyone has the same issue in the future and is looking for possible solutions. Carry on.
Original Post Woah, woah, woah, woah, woah there buddy, put down the rage hate. I know the topic of "player falls through terrain" appears over and over via Google. Totally cool with the answers. =) Not here to figure out how to stop it, because at this point in the game if I haven't done that by now I've got problems. For me, the issue isn't a constant. But, oddly, every now and then in gametesting the player will just take a step, and falls. It's not in the same place, just seems random, like they caught a crease in the mesh or something. Probably has happened 10 times in 700 gametests. (approx) Not enough to pull my hair out over, but enough to want to address. So I figure it's simply a glitch, one that I could live with as the occasional bug, but want to know how / if other folks address that in their games. Reason being, the project I'm working on uses an auto save feature, and player position is one of the things saved to playerprefs. That works great as long as things are going fine, but autosave in the middle of a freefall and the player's game is forever ruined. The first thought is to put a death collider just under the terrain, something to the effect of "ontriggerenter fadeout camera, transfer to a predetermined spot on the map, then die." That part would be pretty easy to code. Now, if there's a way to code "transfer to last known position on the terrain" that would be better, but I'm not picky. Just thought I'd ask how other folks handle an issue like this. Thanks, and God bless.
Answer by _1 · Jan 28, 2014 at 09:39 PM
If I understand your question you want a "got to to last known position script" any easier way than doing that is to check the y coordinate under your ground and if the player is there he should go to the coordinate of the player such as this:
function Update(){
//example
//if we are at this point at the y axis we will go up a little bit
if(transform.position.y <= 0){
transform.position.y = 1;
}
}
this code is very simple and choppy and there are lot better ways but make sure to adjust the value depending on the terrain or the platform's y axis hope this helps if this doesn't truly sorry. Good luck :)
Answer by Nanocentury · Jan 29, 2014 at 12:49 AM
It's more a patch than a solution and may need some refinement but:
Vector3 lastGoodPos;
void Update()
{
RaycastHit hit = new RaycastHit();
Ray ray = new ray(transfrom.position,Vector3.Down);
if(Physics.Raycast(Raycast(ray, out hit)))
{
lastGoodPos = transfor.position;
}
else
{
transform.position = lastGoodPos;
}
}
It will use a raycast to determine if there is anything below the player and restore them to an arbitrary position if there isn't.
hey, thanks to both of you. Sorry I didn't say that before!
Your answer
Follow this Question
Related Questions
Colliding with Terrain and Destroying it 1 Answer
GameObject going into terrain. 0 Answers
How Can I Load Massive Terrains In Sections? 0 Answers
Late Rocket Explosion 1 Answer