Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Youngapprentice · Dec 21, 2010 at 02:27 AM · movementconstraintsboundary

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?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Youngapprentice · Dec 21, 2010 at 07:56 PM 0
Share

PLZ HELP! I would really like to know the answer to this dilemma!

avatar image cristhian198 · Sep 22, 2015 at 10:30 PM 0
Share

//restrict player to bounderies if (pos.y > Camera.main.orthographicSize) {

         pos.y = Camera.main.orthographicSize;
     
     }

7 Replies

· Add your reply
  • Sort: 
avatar image
6
Best Answer

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.

Player Movement Boundaries

Hope this helps

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Rammi · May 19, 2014 at 12:11 PM 0
Share

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.

avatar image
2

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

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image blue squirel · Oct 02, 2014 at 06:44 PM 0
Share

pixeye's code works great but if you use it remember to put a semicolon on the variable $$anonymous$$ y =)

avatar image InvincibleMiners · Mar 09, 2015 at 08:18 PM 0
Share

I dont think this code works for Unity 5... It is like WTF is $$anonymous$$Y and all dat crap

avatar image Pingu03 · Apr 13, 2018 at 04:28 AM 0
Share

is this for javascript?

avatar image AppGynsis · Apr 27, 2018 at 05:56 PM 0
Share

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?

avatar image
1

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);
     }
 }
 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
-1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Clamping movement range on X-axis 0 Answers

Object gets stuck outside boundary if pushed 0 Answers

Little problem with Mathf.Clamp as used in Space Shooter Tutorial 8 Answers

Setting boundary for Z axis 1 Answer

Restric the script and movement in specific area 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges