- Home /
Touchscreen Joystick Character Control using PointerEventData
Hi, I am trying to control my character using an onscreen touch joystick. I have written this code with OnPointerDown, OnPointerUp and OnDrag and it works perfectly. The issue I am having is that this does not really replicate a real controller in the sense that if you keep the joystick pressed in a direction on a controller the movement in that direction continues. In my case if you keep the joystick pressed in a direction than it does not generate another event for movement unless i make a change in the direction axis. Is there an event or something i do not know about to do this? I have looked over the unity documentation and google searches but did not find anything usefull on what I am trying to achieve here. Note that this will be an optimised mobile game so I do not want to use fixed update or just update. Any help is apreciated. Thank you!
Update: Added code:
public void OnDrag(PointerEventData ped)
{
//move joystick
//move character
}
public void OnPointerDown(PointerEventData ped)
{
OnDrag(ped);
}
public void OnPointerUp(PointerEventData ped)
{
//reset joystick
}
I had not added more code as it would not be relevant to my current problem. I am moving the character about a unit in the direction of the joystick on each dragging move. The issue i am facing is that OnDrag really works only on drag and i was wondering if there is some way to make it fire events like let's say every 0.5 s while the joystick button is being hold down. At first when writing this code I had asumed that the OnPointerDown does just that ... while it is being hold down would fire events but that is really not the case.
How can we help if you don't post the relevant code?
There's no actual functionality in that code...
Answer by RedDevil · Feb 08, 2017 at 04:28 PM
Since i could not really find a good way to do this with PointerEventData i just changed my code so that i can achieve what i was after. The way I solved the problem is by changing on PointerDown to call a coroutine of a new method called movement where i just do a yield wait for seconds and do the movement on a loop and then i changed OnPointerUp to stop that coroutine when the joystick is not pressed.
Your answer
![](https://koobas.hobune.stream/wayback/20220612110448im_/https://answers.unity.com/themes/thub/images/avi.jpg)