- Home /
Boundaries / movement constraints
Hi, all! I have a space ship in my game. This space ship moves only on the x and y axes. I would like to make it so that the ship cannot leave a certain perimeter in the game, like how ping-pong paddles cannot leave the table or how you can't leave the screen in Galaga (or however you spell it).
Is this achievable? If so, how?
PLZ HELP! I would really like to know the answer to this dilemma!
//restrict player to bounderies if (pos.y > Camera.main.orthographicSize) {
pos.y = Camera.main.orthographicSize;
}
Answer by unitydev0008 · Jan 06, 2011 at 11:30 PM
One thing that may help you out with this is MathF.Clamp. Simply use the transform you wish to restrict and then the min and max values of that transform you wish the paddle to move between. This is probably not the best way, i have used it and have found it to sometimes cause the paddle to twitch when reaching the boundaries. So if anyone knows of a better way of doing it i also would like to know.
C# Example:
private float minX = -10; // Bottom of the screen private float maxX = 10; // Top of the screen
Update() { MathF.Clamp(transform.position.x,minX,maxX);
// Your code to make the paddle move up and down
}
again i am still new to unity scripting and if this is not an efficent way or the best way i apologize just trying to help out =D
EDIT:
Just found this forum topic and thought it might be of use to both of us, it uses the MathF.Clamp as mentioned above but uses it inside its own function rather than in update which may be more effiecent.
Hope this helps
Yes, but this makes the object hopping on the edge of the screen if you put it into FixedUpdate. If you put it in Update, then it makes the player control somewhat laggy.
Answer by Pixeye · Jan 29, 2011 at 06:56 AM
You can use also something like this
var minX = -10; //left boundary var maxX = 10; //right boundary var minY = -10 // up boundary var maxY = 10; // down boundary
function outofbounds() { if (transform.position.x < minX) { transform.position.x = minX; } if (transform.position.x > maxX) { transform.position.x = maxX; }
if (transform.position.y < minY) { transform.position.y = minY; } if (transform.position.y > maxY) { transform.position.y = maxY; }
}
function Update() { outofbounds(); }
maybe this is no an efficient way but it shuerly works. Its just an exaple of how yo can do your boundaries staff
pixeye's code works great but if you use it remember to put a semicolon on the variable $$anonymous$$ y =)
I dont think this code works for Unity 5... It is like WTF is $$anonymous$$Y and all dat crap
i tried this approach but it looks like my player stuck in the middle of the screen it can't reach even near that border despite i triple checked the x and y coordinates . and don,t know what the reason?
Answer by BelinExperience · Mar 16, 2017 at 08:38 PM
I know it's an old question, but it gets hits on google when I tried to research this same question.
This is my own solution for a similar problem.
Place a mesh in the scene with this script attached. You can have the mesh follow the camera, and if it's an ortho camera, you use a square, if it's a perspective camera, use a frustum or a pyramid for the mesh.
using UnityEngine;
// RetainingWall: Attach to a trigger mesh which will keep rigidbody items contained
// by "teleporting" them to the opposite side, without changing the direction of travel
// @author Belin Fieldson @thebelin
public class RetainingWall : MonoBehaviour {
// Constraints by vertex direction
public bool lockX;
public bool lockY;
public bool lockZ;
void OnTriggerExit(Collider col)
{
// The root location of the colliding game object
Transform go = col.transform.root;
// The Inverse Transform point (relative to the container's center)
// of the colliding game Object
Vector3 iTransformPoint = -transform.InverseTransformPoint(go.position);
// Lock the desired vectors
if (lockX)
iTransformPoint.x = transform.position.x;
if (lockY)
iTransformPoint.y = transform.position.y;
if (lockZ)
iTransformPoint.z = transform.position.z;
// Move the contained item to the the inverse transform
go.position = transform.TransformPoint(iTransformPoint);
}
}
Answer by Azerial · Apr 26, 2011 at 07:27 AM
Add a wall with a collider...just off camera view. That's how I made it so you can't fall off my map. Not a java able coder though so maybe there's another way.
Answer by virtual02 · Jun 27, 2012 at 08:40 PM
Create a 4 thin (about 50 width) cubes so as they all wrap the scene (top,bottom,left,right of scene boundaries). Turn off the mesh rendering on the cubes which will make them invisible. Finally add a collider to each cube. This will make your ship stop when it hits the boundary. You might want to get fancy and use triggers to make the ship move around smoothly instead of crashing into the wall.