• 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 · 4 days ago

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.

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

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

Can I customize the resolution window that appears at the start of my executable? 2 Answers

determine how much space is left on screen 1 Answer

Splitting the game view - part camera, part opaque navigational panel at runtime 1 Answer

How do I set an Android applications resolution? 3 Answers

How to draw a small circle on scene and moving it wherever mouse cursor moves 2 Answers

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