- Home /
Joystick triggers directed function
I started learning about virtual joysticks today and I have question about triggering actions with them. For example I want to trigger action(e.g roll) towards the direction of stick, if I exceed the value of axis(1 or -1). How would I do that?
Answer by JulienBem · Apr 25, 2021 at 06:59 PM
Hello conguerror,
I'm a niewbie in unity but i think you can use "touchDeltaPosition". exemple (I haven't tested this script, it just serves as an idea.) :
public Vector2 startPos;
public Vector2 direction;
public float direction;
void Update()
{
if (Input.touchCount >0 && Input.GetTouch (0).phase == TouchPhase.Began)
{
Touch touch = Input.GetTouch(0);
startPos = touch.position;
}
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
{
direction = touch.position - startPos;
}
if (direction > /*your value*/)
{
/*your action*/
}
}
You can find documentation for touch phase here TouchPhase.Began. If you put the localization of your joystick, and you use the "touchDeltaPosition", i think you can do what you want. Hope this answer can help you. Regards
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Mobile player controller assistance 1 Answer
Change a object position to a specific axis 0 Answers
Game will not load scene on MOBILE, but work fine on PC 0 Answers