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
2
Question by nypraise · Feb 07, 2014 at 06:33 PM · screendraganddropconstraintconstrain

How can I restrict/constrain my drag and drop of sprites to only be on the screen?

I am trying to keep the sprites I drag and drop to be only in the screen. I don´t want it to be possible to drag and drop outside of the screen view. Is it a good way to solve this so I can change the screen size without any problems later?

This is my code so far :

         void OnMouseDown ()
         {
 
                 screenPoint = Camera.main.WorldToScreenPoint (gameObject.transform.position);
 
         }
 
         void OnMouseDrag ()
         {
                         Vector3 currentScreenPoint = new Vector3 (Input.mousePosition.x, 60, screenPoint.z);
                         Vector3 currentPos = Camera.main.ScreenToWorldPoint (currentScreenPoint);
                         transform.position = currentPos;
 
 
         }


This is to use a drag and drop constraint to the X axis to make my own slider. Now, I need it to stop where the screen ends on both sides of the X axis.

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 Mike-Geig ♦♦ · Feb 08, 2014 at 09:06 PM 0
Share

Are you talking in-game or in the scene view?

avatar image nypraise · Feb 10, 2014 at 02:53 PM 0
Share

I solved it. See answers under. I was talking about in-game. Thank you for seeing my tweet and checking it out. @$$anonymous$$ike Geig

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by nypraise · Feb 09, 2014 at 12:37 AM

I solved it with using a Mathf.Clamp. First I got confused with Mathf.Clamp because of the documentation mentions Time.time etc. But after watching Will Goldstones tutorial on it I figured it out : http://www.youtube.com/watch?v=ZggxVn93ePI

Here is my C# code for it :

         void Update(){
 
         transform.position =  new Vector3(Mathf.Clamp(transform.position.x, -5.0f, 5.0f),0,0);
 
         }


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 Mr_yilanci · Jan 11, 2021 at 06:52 PM

Thanks man .... you saved my time .... you are king

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

20 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

Related Questions

Orientation Problem 1 Answer

GUITexture aspect ratio resize 1 Answer

How do I rotate an object on one axis to face android touch? 0 Answers

Object resolution 0 Answers

How do I set up my editor to "ipadview" 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