• 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 MSavioti · Jan 05, 2019 at 07:48 PM · uidraggingrestrictmovement

Restraining moving area of a draggable UI element

scene image

The image I sent is from one of the game modes of an educational game that I'm making. This mode would consist in dragging that purple object horizontally between the two red gizmos and never vertically. I've succeded in making it draggable, but none of my attempts have been successful in restraining the movement in the way I want to.

 public void OnDrag(PointerEventData eventData) {        
         Vector2 localPos = Vector2.zero;
         RectTransformUtility.ScreenPointToLocalPointInRectangle(rect, Input.mousePosition, Camera.main, out localPos);
         transform.position = transform.TransformPoint(localPos);
     }

This game is intended to be used on smartphones and the proper conversions will be made in the future, so I could consider moving the object by screen buttons or swipes if thing don't go my way.

Can anyone help me?

screen-shot-01-05-19-at-0530-pm.jpg (33.0 kB)
Comment
Add comment · Show 8
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 JxWolfe · Jan 05, 2019 at 07:54 PM 0
Share

why don't you drag it like you have been doing, then just clamp down on the axis?

 float startingX;
 void Start()
 {
   startingX = transform.position.x;
 }
 
 void OnDrag()
 {
   //move and stuff
   transform.position = new Vector3 (startingX,transform.position.y,transform.position.z);
 }
avatar image MSavioti JxWolfe · Jan 05, 2019 at 08:02 PM 0
Share

I tried that and had strange results, probably because it works with RectTransform and not Transform. I've messed around with the RectTransform methods, got different results, but nothing close to what I need.

avatar image JxWolfe MSavioti · Jan 05, 2019 at 08:14 PM 0
Share

hmmmm.....

  public void OnDrag(PointerEventData eventData) {        
          Vector2 localPos = Vector2.zero;
          RectTransformUtility.ScreenPointToLocalPointInRectangle(rect, Input.mousePosition, Camera.main, out localPos);
          transform.position = transform.TransformPoint(new Vector2(0,localPos.y);
      }

?

Show more comments

1 Reply

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

Answer by MSavioti · Jan 05, 2019 at 09:56 PM

Thanks to @JxWolfe (thank you very much!), I arrived in a solution:

 public class PlayerScript : MonoBehaviour, IDragHandler {
     [SerializeField]
     public Image image;
 
     private float leftLimit;
     private float rightLimit;
     private RectTransform rect;
 
     private void Start() {
         rect = GetComponent<RectTransform>();
     }
 
     public void OnDrag(PointerEventData eventData) {
         Vector2 localPos = Vector2.zero;
         RectTransformUtility.ScreenPointToLocalPointInRectangle(rect, Input.mousePosition, Camera.main, out localPos);                
 
         if (transform.TransformPoint(new Vector2(localPos.x, 0)).x >= leftLimit
                 && transform.TransformPoint(new Vector2(localPos.x, 0)).x <= rightLimit) {
 
             transform.position = transform.TransformPoint(new Vector2(localPos.x, 0));
         }
     }
 }
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 JxWolfe · Jan 05, 2019 at 09:58 PM 0
Share

glad to be of help! $$anonymous$$eep on program$$anonymous$$g!

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

166 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 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 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 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 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 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 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 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

Keep Draggable UI window within Canvas? 1 Answer

Drag object and position(snap) on another similar one using Raycasting? 0 Answers

NGUI Mouse position doesn't match with dragged object 0 Answers

SteamVR Slider won't drag using VR Controller,SteamVR-controller can't drag Slider handle 0 Answers

UI button mobile - want a button drag to register as a click 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