- Home /
How to keep players from falling off the map
Hi, I have a problem... Whenever I am testing my game I can just walk off the map... How do I fix this? Thanks
Clamp the position of the player :
Answer by TheRichardGamer · Aug 07, 2013 at 05:16 PM
Try making invincible walls, most games use invincible walls to prevent things like that.
Answer by creighcl · Aug 07, 2013 at 05:48 PM
If you're using the physics engine, you can create an empty gameobject and attach a Rigidbody and a Collider.
Just expand that collider to create an invisible wall that cannot be passed through.
If it's not a physics based game, you'll have to get a little more creative based on how you have setup your movement.
Answer by hukondejo · Jun 26, 2017 at 02:13 PM
var groundLimit : int;
var XOrigin : int;
var ZOrigin : int;
var height : int;
if (height < 0){
print("FALLPREVENT:Height must be more than 0.");
height = 0;
print("FALLPREVENT:Setting height to 0.");
}
else
{
transform.position = Vector3(XOrigin,height,ZOrigin);
}
function Update ()
{
if (transform.position.y < groundLimit){
transform.position = Vector3(XOrigin,height,ZOrigin);
}
}
When you begin falling this respawns you in the given coordinates. You can set the following values.:
Ground Limit - if You reach this height limit you will respawn. must be a negative value. Height - your height position after respawn. must be a positive value. X and Z origin - your position after respawn.
Note : The given coordinates will be your ingame starting point. You can attach it to any gameobject what uses gravity.
This person might not use javascript. You should keep a c# version on hand.
Here is an updated version of of the script. Its in c# and it grabs any needed value from the gameobject automaticaly. Attach it to any gameobject what uses gravity.
public class FallPrevent : $$anonymous$$onoBehaviour
{
// Attach this script as a component to the character
private float Xorigin;
private float Zorigin;
private float Height;
private void Awake()
{
// Setting Startpoint
Xorigin = transform.position.x;
Zorigin = transform.position.z;
Height = transform.position.y;
// Asking Height
if (Height < 0)
{
print("FALLPREVENT: Height is bellow zero! Setting secure height.");
Height += 3;
}
}
void Update ()
{
// Character respawns to the starting point when falling.
if (transform.position.y < 0)
{
transform.position = new Vector3(Xorigin, Height, Zorigin);
}
}
}
Answer by Linkthehylian04 · Jun 26, 2017 at 02:32 PM
Create an empty GameObject, name it "Wall", give it a box collider, then use it as an invisible wall so that you can't fall off the edge.
Your answer
Follow this Question
Related Questions
In Game Animation problem 0 Answers
Assigning UV Map to model at runtime 0 Answers
First Person controller problem 1 Answer