• Unity
  • Services
  • Made with Unity
  • Learn
  • 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
  • Forums
  • Answers
  • Feedback
  • Issue Tracker
  • Blog
  • Evangelists
  • User Groups

Navigation

  • Home
  • Unity
  • Industries
  • Made with Unity
  • Learn
  • Community
    • Forums
    • Answers
    • Feedback
    • Issue Tracker
    • Blog
    • Evangelists
    • User Groups
  • Get Unity
  • Asset Store

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 ShadowX · Nov 08, 2011 at 04:12 PM · androidiosclamp2.5dtop down

Clamp player to screen borders

Hi guys,

Im working on 2.5D top down shooter for iOS and Android and I need to clamp player position to avoid the player leave game screen. My game setup is camera, moving along Z axis at 60 degrees angle and the player is moving along x,z axis.

Is there any way how to overcome this?

Comment
Add comment · Show 1
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 Muzz5 · Nov 08, 2011 at 04:54 PM 0
Share

Invisible walls?

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by BarkShark · Nov 08, 2011 at 09:10 PM

Alright, I've figured it out, apparently I made a stupid mistake.

This is the new script, I tested it and it works as it should be. Make sure the camera that is rendering the player is the main camera. Else you have to change the script a little bit.

 function Update () 
 {
 
 
     
         var dist =(transform.position.y - Camera.main.transform.position.y);
         
         var leftLimitation = Camera.main.ViewportToWorldPoint(Vector3(0,0,dist)).x;
         var rightLimitation = Camera.main.ViewportToWorldPoint(Vector3(1,0,dist)).x;
     
         var upLimitation = Camera.main.ViewportToWorldPoint(Vector3(0,0,dist)).z;
         var downLimitation = Camera.main.ViewportToWorldPoint(Vector3(0,1,dist)).z;
     
 
     
     
     transform.position.x = Mathf.Clamp(transform.position.x,rightLimitation,leftLimitation);
     transform.position.z = Mathf.Clamp(transform.position.z,downLimitation,upLimitation);
 }


Image so you can understand the principe:

125alt text125

The principle remains the same:

-First we create a variable dist, this variable calculates the distance between the players y position and the y position of the camera.

  • Then we create four variables, leftLimitation and so on. Camera.main.ViewportToWorldPoint calculates the cameras borders and converts them to world coordinates so we can restrict the players transform. We do this both for the x and z axis because these are the axis the player moves along.

  • List item

  • Then we create a function that restricts the players position both for the x and z axis.

I hope this solves your problem, sorry for my previous wrong answer.

Comment
Add comment · Show 6 · 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 ShadowX · Nov 08, 2011 at 09:32 PM 0
Share

Thank you for your time! I really apprecite it. Also thank you, that you are explaing the code! Your code would work if you have camera at 90 Degrees(2D).But I position camera at 60 degrees to use 3D effect(2.5D). Im not sure if I making myself clear :/

avatar image BarkShark · Nov 08, 2011 at 09:38 PM 0
Share

Yes you do, I'll see if I can help you any further tomorrow.

avatar image BarkShark · Nov 09, 2011 at 11:58 AM 0
Share

Im really sorry but I don't know a solution to your problem if you use 2.5D.

avatar image ShadowX · Nov 09, 2011 at 12:12 PM 0
Share

Thank you anyway!

avatar image Ziron999 · Jan 21, 2014 at 11:44 PM 1
Share

i've been trying to re-write this in C#

     float dist = (transform.position.y - Camera.main.transform.position.y);
     float leftLimitation = Camera.main.ViewportToWorldPoint(new Vector3(0,0,dist)).x;
     float rightLimitation = Camera.main.ViewportToWorldPoint(new Vector3(1,0,dist)).x;
     
     float upLimitation = Camera.main.ViewportToWorldPoint(new Vector3(0,0,dist)).z;
     float downLimitation = Camera.main.ViewportToWorldPoint(new Vector3(0,1,dist)).z;

     float tempx = Mathf.Clamp(transform.position.x,rightLimitation,leftLimitation);
     float tempz = Mathf.Clamp(transform.position.z,downLimitation,upLimitation);
     transform.position = new Vector3 (tempx, transform.position.y, tempz);

however the 3d object just flickers from top right to bottom left weird?!

Show more comments

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Limit the position of the object, but still keep following the touch? How to use ClampMagnitude properly? 1 Answer

Mobile 2.5D MMO Game Development Advice 0 Answers

How to get some background process time after OnApplicationPause 1 Answer

WWW Post (Form) not working on mobile. 1 Answer

How to get current battery life on mobile device 3 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges