I want to start by saying that Input and Touches are not crappy. They were still useful and were the best way to test touch on mobile devices before OnPointerDown and OnBeginDrag . OnMouseDown() you can call crappy because it is not optimized for mobile devices. For a beginner who has just begun to learn Unity, Input and Touches are their parameters.
As for your question, OnPointerDown and OnBeginDrag NOT , the same. Although they almost do the same, they were implemented to perform in different ways. Below I will describe most of them:
OnPointerDown : Called by pressing / touching the screen (when a button or finger is pressed on the touch screen)
OnPointerUp : Called when pressed / touched (when the click is released or the finger is removed from the touch screen)
OnBeginDrag : Called once before dragging (when moving the finger / mouse down for the first time)
OnDrag : Repeatedly called when the user drags the screen (when the finger / mouse moves around the touch screen)
OnEndDrag : Called when the drag stops (when the finger / mouse is no longer moving on the touch screen).
OnPointerDown compared to OnBeginDrag and OnEndDrag
OnPointerUp will be called NOT if OnPointerDown not . OnEndDrag will be called NOT if OnBeginDrag not . It's like braces in C ++, C #, you open it ' { ', and you close it> } ".
DIFFERENCE: OnPointerDown will be called once and immediately when the mouse pointer / mouse is displayed on the touch screen. Nothing else will happen until a mouse movement or finger movement appears on the screen, then OnBeginDrag will be called once , and then OnDrag.
They are designed for use in extended usage such as the custom user interface with controls that are not included in Unity.
WHEN USE EACH ONE:
1 .. When you need to perform a simple button , for example, the Up, Down, Shoot buttons on the screen, you only > need OnPointerDown to detect a touch. This should work for sprite images.
2 .. When you need to implement a custom switch and you want it to be realistic , so that the player can drag left / right or up / down to toggle , then you need OnPointerDown , OnBeginDrag , OnDrag , OnEndDrag , OnPointerUp . You need to write your code in this order so that a smooth transition appears on the screen. For some toggle switches, a click is used and it will switch . Some people prefer to do it realistically, making it so that you drag it to switch it.
3. Also, if you want to embed a popup that you can drag and drop, you also need to use these 5 functions ( OnPointerDown , OnBeginDrag , OnDrag , OnEndDrag , OnPointerUp ). First, when there is a click ( OnPointerDown ), make sure that the sprite click is the one you want to move. Wait for the player to move ( OnBeginDrag ) with your finger / mouse. As soon as they start dragging, perhaps you can call the coroutine function using the while loop , which starts moving Sprite inside this coroutine too, you can smoothly move Sprite with Time.deltaTime or in any other preferred way.
Since OnBeginDrag is called once, this is a good place to start a coroutine . When a player continues to drag Sprite, OnDrag will be called again . Use the OnDrag function to get the current location of the crawler and update it to Vector3 , which will be executed when the already accompanying version is executed > sprite position . When the player stops by moving the finger / mouse on the screen, OnEndDrag is OnEndDrag and you can boolean variable and tell the coroutine to stop the update , the Sprite position. Then, when the player releases his finger ( OnPointerUp ), you can stop the coroutine using the StopCoroutine function.
Due to OnBeginDrag we can start coroutine after the drag started, waiting for the drag to finish. start run , this will accompany in OnPointerDown , because it means that every time the player touches the screen, the coroutine will be launched .
Without OnBeginDrag we must use the boolean variable to force coroutine to run only once in the OnDrag function, which is called every time, or otherwise a coroutine will be executed, and the sprite will move unexpectedly.
4. If you want to determine how long the player has moved his finger. An example of this is the famous game called Fruit Ninja . Allows you to simply say that you want to determine how far the player has slipped on the screen.
Firstly, wait until OnPointerDown is called, wait until OnBeginDrag is called, then you can get the current position of the finger inside OnBeginDrag , because OnBeginDrag is called before the finger starts to move. After releasing the finger, OnEndDrag is OnEndDrag . Then you can again get the current position of the finger. You can use these two positions to check how far your finger is moved using subtraction .
If instead you decide to use OnPointerDown as a place to get the first position , you will get the wrong one because if the player moves to the right , then he waits and removes the left one , and then he waits and sweeps his finger up without releasing his finger after each , the only good result: the first miss (right napkin). The icon on the left and up will have invalid values, since this is the first value that you received when OnPointerDown was called this value, which you still use. This is due to the fact that the player never removed his finger from the screen, therefore , therefore , OnPointerDown never called again and the first old old meaning still exists.
But if you use OnBeginDrag instead of OnPointerDown , this problem will be gone , because when the finger stops moving , OnEndDrag is OnEndDrag and when it starts moving again OnBeginDrag is called again so that the first position is overwritten with a new one .