Boundaries/Movement Constraints

Hi, all! I am almost done with my first game and I need just this answer and I will be done! YAY!

i have a ship in my game that moves only on the x and y axes. I would like to make it so that the ship cannot leave the screen (or any boundary I have set up) like how in Ping-Pong the paddles cannot leave the board.

How would I achieve this?

Probably wait for someone else to comment as this probably not the best but something like this might work.

Vector3 bottomLeft  = camera.ScreenToWorldPoint(new [URL="http://unity3d.com/support/documentation/ScriptReference/Vector3.html"]Vector3[/URL](0, 0, camera.nearClipPlane));

Vector3 topRight = camera.ScreenToWorldPoint(new [URL="http://unity3d.com/support/documentation/ScriptReference/Vector3.html"]Vector3[/URL](camera.pixelWidth, camera.pixelHeight, camera.nearClipPlane));

if (target.transform.postion.x > bottomLeft.x)
{

   allowed

}
else
{

   notallowed

}

Would this work for y as well?

Man! I can’t believe Unity wouldn’t implement something so simple as movement boundaries into their syntax!..
they must’ve…

i mean, Blender even has it…

1 Like

Yeah it should work, but it all depends on the view of the camera, if you are using a perspective view then you need to make sure you replace camera.nearClipPlane with the transform.position.z as objects further away are allowed to be much higher than objects that are closer.

If it’s ortho, then you should be fine with the script above.

My camera is perspective…

That’s cool you just need to find out how far away your object is from the camera first:

float howFarAway = Vector3.Distance(camera,myObject).z;

Vector3 topRight camera.ScreenToWorldPoint(new [URL="http://unity3d.com/support/documentation/ScriptReference/Vector3.html"]Vector3[/URL](camera.pixelWidth, camera.pixelHeight, howFarAway));

Then it should give you a Vector3 that tells you the furthest an object can go right and up if it’s “howFarAway” from the camera.

And when it gives me these values, what do I plug them into to make the boundaries?

No, restricting the motion of an object is too specific a feature to be built into a general-purpose game engine such as Unity. What restricting an object’s motion means exactly will vary from game to game; as such, it’s really up to the developer to implement features like this.

As for your problem, how are you moving the object? Is it a rigid body? Are you using the character controller component?

No and no. I am simply making if statements on Input.GetAxis and then having my ship move up, down, left, or right * Time.deltaTime*speed (a variable i set)

You could place invisible colliders at the edge of the screen, then you don’t need to worry about boundaries.

1 Like

It should be as simple as (e.g.):

transform.position.x = Mathf.Clamp(transform.position.x, minX, maxX);

(Note that the above would have to be expressed differently in C#.)

If that isn’t working for you, perhaps you could post your code and describe in what way it’s not working.

Is that in Javascript, or boo?

Do this should work?

var controlspeed = 5.0;
var explosion : GameObject;
function Update () {
	var x =Input.GetAxis("Horizontal")*Time.deltaTime*controlspeed;
	var y =Input.GetAxis("Vertical")*Time.deltaTime*controlspeed;
	transform.Translate(x,y,0);
	transform.position.x = Mathf.Clamp(transform.position.x, (transform.position.x + 9), (transform.position.x-9));

}

function OnCollisionEnter(collision : Collision){
	
	  if(collision.gameObject.tag == "Pickup")
    {
        MakeBlast.shots += 5;
        audio.Play();
    }
	
	if(collision.gameObject.tag == "Asteroid")
	
	{
		
		Instantiate(explosion,transform.position,transform.rotation);
	
		Destroy(gameObject);
	
		Application.LoadLevel("Gameover");
}
}

Just consider it to be pseudocode, and adapt it as needed for your language of choice. (If you run into problems, tell us what language you’re using and what problems you’re encountering.)

This:

transform.position.x = Mathf.Clamp(transform.position.x, (transform.position.x + 9), (transform.position.x-9));

Probably doesn’t do what you want. First of all, I’m not sure what Mathf.Clamp() does when the input range is inverted (that is, min > max). Also, there’s likely little point in clamping ‘x’ to a range that’s relative to ‘x’. (My earlier example shows how you’d clamp ‘x’ to a set range.)

That said, it looks like you’re using colliders, which suggests that you may not want to be moving your object using Translate() anyway.

So is transform.position.x real code or pseudo code?

What programming language are you using?

Javascript

As far as I know, that code will work as is in UnityScript (aka javascript). However, I don’t use UnityScript myself so I can’t say for sure. (You could just try it out and see if it works though. Or, maybe someone else can confirm its correctness.)

Edit: Just to be clear, the code I’m referring to (and the code I’m assuming you’re referring to), is this code:

transform.position.x = Mathf.Clamp(transform.position.x, minX, maxX);

That’ll work in both java and C#.