How to prevent Character Controller from falling of the edge?
As mention above, is there a way to prevent the character controller from walking to it's death if there is no more ground to walk?
I have find a similar question as posted here:
http://answers.unity3d.com/questions/37407/character-controller-problem.html
But i have no idea on how to use it to my advantage. Can anyone be kind enough to advice me on that?
other than pathfinding, just put a collider on the edge, this will act as an "invisible wall"
Thanks for the reply. Is it possible to do it programmatic way? By the way, what is the pathfinding thingy u mention about? Care to shed some light on me?
'pathfinding' is the arcane art of writing programs which can locate and avoid obstacles between themselves and their goals. It's pretty complicated.
As for doing it programmatically, it really depends on the exact structure of the mesh you are walking on. For some things it would be relatively straightforward, but for most it's pretty hard to arbitrarily decide whether a player should be able to walk off something (is it a balcony, or is it a drop into the void? The program doesn't know!)
Is there really no way to just turn off gravity? I feel like that may be what some people are looking for. I know that's what I'm looking for.
Edit: I know you can turn it off in the project, but unfortunately when I do that, I can no longer control the character. They won't move if there is 0 gravity. It's very frustrating. Especially when Rigidbody has a "Use Gravity" check box, which doesn't seem to me to do anything at all on a character.
Answer by NickLD · Dec 30, 2014 at 11:01 AM
to do it in script, I would first make a raycast a short distance in front of you pointing downwards. if the ray returns a false(meaning it has hit no colider) then ignore player forward input key. That way they cant go over the edge.
this works, but not provide a smooth movement because player stops suddenly on reach the edge
Answer by supergamespawn · Oct 06, 2011 at 03:59 PM
the easy way is to make a cube into a wall and where you can change the cubes texture or color(sorry i forgot what its called)find a cloro thats invisible and the player will walk into the cube and not fall off
Answer by Sindorej · May 11, 2014 at 12:11 PM
This is an old post, but for people searching for this the solution is to create an EmptyObject and attach a Collider to it.
Answer by OZAV · Apr 25, 2016 at 02:48 PM
. ..and, again, as i always say, this is kinda thread that will NEVER get old, as long a people make games in Unity. *Good that admins did NOT have it closed, thanks. So, here, (for now, and all future readers of this thread): you need to make in CODE, and define IN CODE a Square Matrix, after which bound player is considered out of it. Here's the (my simpler version) you can use, and additionally modify, for the task (you only MUST drag your center of your each map each time to be 0,0,0, in coordinates, for using this script in SUCCESSFUL manner) So, any objects you want to limit how far, or high it can go, just attach this script to it, and enter desired limits in Inspector: (*you may also add your damage call line, later, as well): here, my clean "LimitWorldBoundaries" script: LimitWorldBoundariesScr
Answer by hukondejo · Oct 17, 2017 at 05:53 AM
I've answered in a similar thread, but I think this script will be useful to this issue too.
The script prevents the falling. While your terrain will be generated in 0 height (yaxis) at start everything whats bellow zero height(yaxis) will be automaticaly respawnned to their starting point. You can use it not just on the player but everything else what uses gravity.