Drag Object from left to right ONLY
Hello,
I am trying to drag my object from left to right ONLY, problem is I can drag it left, right, up and down, and this is not the behaviour I am looking for. Is there a way to constrain the x and y values from the vector 3? Can anyone please guide me in the right direction? Thank you for your help. My code is as follows:
Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance); Vector3 objectPos = Camera.main.ScreenToWorldPoint(mousePos); transform.position = objectPos; }private void OnMouseDrag(){
Answer by EddyDavila · Oct 19, 2018 at 04:43 AM
I found the answer. It might be a bit "dirty" but it works. I simply hardcoded the value from input.mouseposition.y to a static value so when I drag the object it takes the x value from the mouse only from side to side by NOT assigning a new mouse location on the y mouse location. Hope it helps.
Answer by y0kkaboom · Oct 19, 2018 at 09:14 AM
@EddyDavila I Think you can, with a rigidbody, constrain the X, Y or Z position. Then just select the axis corresponding on your "Left to right" and check the other 2 Hope i'm helping!