- Home /
Dont let an object pass the screen sides
So , i have an object which can be moved by mouse dragging. the object can only move left or right but when it moves too left or too right it passes the wall and the object is invisble..
P.S :With C# and I need it to work on all the resolutions
Vector3point=Camera.main.ScreenToWorldPoint(Input.mousePosition);
point.z = gameObject.transform.position.z;
point.y=gameObject.transform.position.y;
gameObject.transform.position = point;
Answer by homer_3 · Jan 13, 2015 at 10:58 PM
I think this post will do what you need http://answers.unity3d.com/questions/798586/limit-movement-of-object-based-on-screen-size-unit.html
Still cant get this to work.. How could i make this to not pass the screen sides?(I need only limit on the X asix) Vector3point=Camera.main.ScreenToWorldPoint(Input.mousePosition); point.z = gameObject.transform.position.z; point.y=gameObject.transform.position.y; gameObject.transform.position = point;
What code did you try? You just pasted the same code in your OP again. Did you look at the code in the link?
I tried this: Vector3 pos = Camera.main.WorldToViewportPoint(Input.mousePosition); pos.x = $$anonymous$$athf.Clamp(pos.x, 0.07f, 0.93f); pos.y = $$anonymous$$athf.Clamp(pos.y, 0.07f, 0.03f); gameObject.transform.position = Camera.main.ViewportToWorldPoint(pos);
But when im trying to drag the object he keep sticking to the right-down corner of the screen
In your 1st line you should be using gameObject.transform.position not the mouse position. You can set the gameObject's position to anything you want before the bounding code. Then that bounding code will push it into view of the camera. Also, if you don't care about bounding the Y coordinate, you can leave that line out.
About the 1st line , i would like to move the object by mouse dragging it..so i must to use the mouse pos, right?
Answer by SheZii · Feb 07, 2019 at 08:23 AM
If you want to clamp/restrict gameobjects on the boundary of Minimap, here are the links
Youtube: https://www.youtube.com/watch?v=xW4zZQhRKlI
Step by step tutorial: http://techscenarios.com/unity3d-clamp-objects-on-edges-of-minimap-clamp-on-minimap-boundary-free-scripts-part-4/
Your answer

Follow this Question
Related Questions
Alpha blending with more than one pass 0 Answers
How to pass data through scenes? 0 Answers
How to draw a mesh multiple times with different shader parameters 1 Answer
when mouse pass over a button,the console print some content 1 Answer
Get color of pixel before pass: prefer cg, shaderlab is last resort 0 Answers