- Home /
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.
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
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);
}
Your answer
Follow this Question
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