Save object position after drag?
I have this script that currently makes it possible to drag a panel up and down and restrict the drag to a certain area, but depending on where i click the panel teleports to another position and then I can drag normally (If I click the panel at the top it teleports up). How can i make the panel stay in the position that i left it on no matter where i click and then drag it normally?
bool pressed = false;
public void OnPointerDown(PointerEventData ped)
{
pressed = true;
}
public void OnPointerUp(PointerEventData ped)
{
pressed = false;
}
void Update()
{
if (pressed)
{
Vector2 pos = Input.mousePosition;
pos.x = transform.position.x;
pos.y = Mathf.Clamp(pos.y, 90, 347);
transform.position = pos;
}
}
Comment
Thanks for the answer, changed it to this code, but it doesnt work unless i add On$$anonymous$$ouseDrag() to the update function, and if I do that it drags my object even if $$anonymous$$ouse isnt pressed. I tried to use On$$anonymous$$ouseUp() but that doesnt work at all. How can i get this to work?
void Update() { // On$$anonymous$$ouseDrag(); }
void On$$anonymous$$ouseDrag()
{
Vector2 pos = Input.mousePosition;
pos.x = transform.position.x;
pos.y = $$anonymous$$athf.Clamp(pos.y, 90, 347);
transform.position = pos;
}
}