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
0
Question by keshk · Jan 02, 2014 at 02:07 AM · c#transformposition

Restrict movement along x-axis

I am able to restrict the movements successfully with the following codes. But I wanted to make it suitable for any screen size thus wanted to restrict the movement range according to screen size but it is not working. The objects origin is (0,0,0). Thanks for any advice.

 //Working code where I input exact figures
 if (transform.position.x <= -6) 
         {
             transform.position = new Vector3(-6, transform.position.y, transform.position.z);
         }
         if (transform.position.x >= 6)
         {
             transform.position = new Vector3(6, transform.position.y, transform.position.z);
         }
 
 //Not working. I am trying to use screen size here. 
 if (transform.position.x <= (Screen.width/2) * -1)
         {
             transform.position = new Vector3((Screen.width/2) * -1, transform.position.y, transform.position.z);
         }
 
         if (transform.position.x >= (Screen.width / 2))
         {
             transform.position = new Vector3((Screen.width / 2), transform.position.y, transform.position.z);
         }
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 robertbu · Jan 02, 2014 at 02:08 AM 0
Share

You are mixing screen coordinates with world coordinates. You need to do all of your calculations in world coordinates. Is this a perspective camera or an orthographic camera?

avatar image keshk · Jan 02, 2014 at 09:35 AM 0
Share

This is for orthographic camera. Would like to know about perspective too for knowledge sake if possible. :)

3 Replies

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

Answer by JNetRocks1316 · Jan 02, 2014 at 04:06 AM

The best way to do this is to use ScreenToWorldPoint to take an area 'seen' by the camera and turn it into a 3D world point.

With an orthographic camera this is simple because depth becomes a non issue.

 using UnityEngine;
 using System.Collections;
 
 public class Example : MonoBehaviour {
 
     private Vector3 topLeftBoundary = Vector3.zero;  //Will hold the topleft corner of the screen position.
     private Vector3 bottomRightBoundary = Vector3.zero;  //Holds the bottom right corner.
 
 
     // Use this for initialization
     void Start () {
         //Set the boundary information to the actual screen positions. 0, 0 is top left corner.
         topLeftBoundary = camera.ScreenToWorldPoint(new Vector3(0, 0, camera.farClipPlane));
         bottomrightboundary = camaera.screenToWorldPoint(new Vector3(Screen.width, Screen.height, camera.farClipPlane));
 
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 }

However, if you want a perspective camera you'll need to change "camera.farClipPlane" to the z-depth of the point in relation to the camera. You'll want to pull it to the location of the player so something like player.transform.z would go in there instead.

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 TheShadowblast123 · Jan 02, 2014 at 02:57 AM

you could get a pixel to meter ratio, plug that in and use it to modify a Mathf.Clamp to your needs. //Understand that this is my tenth Time messing around with this so I'm a little angry //javascript for future passerbyers

  public var holdMyRatio : float;// set to pixel to meter ratio
  private var holdMyScreenWidth : float;//set to screen width (this has to be done in awake)

  function Awake () {

       holdMyScreenWidth = (Screen.Width / 2) * holdMyRatio;

  }

  function Update () {

       transform.position = new Vector3(Mathf.Clamp(transform.position.x, -holdMyRatio, holdMyRatio), transform.position.y, tranform.position.z);

  }
  //C# 
  //Insert the usings and class here
  public float holdMyRatio;
  private float holdMyScreenWidth;

  void Awake () {

       holdMyScreenWidth = (Screen.Width / 2) * holdMyRatio;

      }

  void Update () {

       transform.position = new Vector3(Mathf.Clamp(transform.position.x, -holdMyRatio, holdMyRatio), transform.position.y, tranform.position.z);

  }

you could just use Mathf.Clamp to simplify your code to two lines

Comment
Add comment · Show 3 · 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 keshk · Jan 02, 2014 at 09:42 AM 0
Share

Hi, I don't understand the portion where I set pixel to meter ratio. How do I calculate this value?

Also for the Awake method, I don't get what is going on there. It is just a variable being assigned a value. I don't get what is the action that is happening there due to this assignment.

Sorry if noobish questions.

avatar image keshk · Jan 02, 2014 at 09:54 AM 0
Share

$$anonymous$$athf.Clamp is not working for me even if I use exact values ins$$anonymous$$d of screen size as follows:

Vector3 pos = transform.position; pos.x = $$anonymous$$athf.Clamp(transform.position.x, -6,6);

avatar image karthik007 · Apr 29, 2016 at 05:14 PM 0
Share

thanks TheShadowblast123, It helped me a lot!!!!!

avatar image
0

Answer by sath · Jan 02, 2014 at 03:24 PM

is this what u need?

http://answers.unity3d.com/questions/62189/detect-edge-of-screen.html

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

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

21 People are following this question.

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

Related Questions

C# move y position of object not working 2 Answers

How to place the GameObjects in a sequence on the plane 1 Answer

just update X and Z position of gameobject 1 Answer

Change Block Position In Block Matching Game C# 2 Answers

Multiple Cars not working 1 Answer


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