Question by
JoniBerg · Apr 19, 2019 at 11:14 AM ·
2d gamedraggingonmousedrag
Dragging on First clicking?
How i check dragging Started on first click?
right now Dragging Start on second click.
First click only created dragging object.
This is 2D game and created object shoud stay on Camera/Canvas.
Code is now:
public class Draging : MonoBehaviour
{
public bool dragging = false;
Vector3 dist;
float posX;
float posY;
void OnMouseDown()
{
dist = Camera.main.WorldToScreenPoint(transform.position);
posX = Input.mousePosition.x - dist.x;
posY = Input.mousePosition.y - dist.y;
}
void OnMouseDrag()
{
Vector3 curPos =
new Vector3(Input.mousePosition.x - posX,
Input.mousePosition.y - posY, dist.z);
Vector3 worldPos = Camera.main.ScreenToWorldPoint(curPos);
transform.position = worldPos;
dragging = true;
print("Trigger Moving");
}
void OnMouseUp()
{
dragging = false;
print("Trigger STOP!");
}
}
Comment
Your answer
Follow this Question
Related Questions
I want to put a limit on the edges of the screen for dragable objects 1 Answer
Cannot call trigger-event when dragging object via touch 1 Answer
Unity2D: OnMouseDrag, OnMouseOver etc. out of a sudden not working for specific GameObject 0 Answers
System Equipment and Changing Clothes in 2d Endless Runner Game ? 0 Answers