- Home /
Change the position of the Joystick by tap
Greetings unity's community. I've been working on a 2D top-down game for mobile, and I added the standard assets' joystick to my project. I made all the necessary changes and it works like a charm now. I also added a roll system, when I drag the Joystick in the same direction twice, the player rolls, and that works fine too.
Now let's get to the problem I'm facing; I want that when the player taps in the restricted area of the Joystick, the joystick moves to that position. Kinda like Zenonia 6 controls if you are familiar with it.
That mechanic would allow the player implicitly to roll more easily by just tapping twice in the same direction. So any ideas for how could I add this feature to my game ?
Answer by Aissasa · May 03, 2016 at 01:52 PM
So I found a solution for this : I worked with Input.touches, get the last touch position, if it's in the restricted area of the joystick, I move the Joystick to that position, in the Began and Stationary phases, and return it to the central position if the phase is Ended.
If anyone is interested, here's the code to that part:
if (Application.platform == RuntimePlatform.Android)
{
Touch[] taps = Input.touches;
if (taps.Length > 0)
{
Touch lastTap = taps[taps.Length - 1];
if (InRistrictedArea(new Vector3(lastTap.position.x, lastTap.position.y, 0)))
{
if (lastTap.phase == TouchPhase.Began || lastTap.phase == TouchPhase.Stationary)
{
Vector3 newPos = new Vector3(lastTap.position.x - m_StartPos.x, lastTap.position.y - m_StartPos.y);
transform.position = Vector3.ClampMagnitude(newPos, MovementRange) + m_StartPos;
UpdateVirtualAxes(transform.position);
}
if (lastTap.phase == TouchPhase.Ended)
{
HandleRoll();
lastDragTime = Time.time;
lastDragPos = transform.position;
transform.position = m_StartPos;
UpdateVirtualAxes(m_StartPos);
}
}
}
}
else // to test on pc
{
if (Input.GetMouseButtonDown(0))
{
Vector2 clickPos = Input.mousePosition;
if (InRistrictedArea(clickPos))
{
Vector3 newPos = new Vector3( clickPos.x - m_StartPos.x, clickPos.y - m_StartPos.y);
transform.position = Vector3.ClampMagnitude(newPos, MovementRange) + m_StartPos;
UpdateVirtualAxes(transform.position);
}
}
if (Input.GetMouseButtonUp(0))
{
HandleRoll();
lastDragTime = Time.time;
lastDragPos = transform.position;
transform.position = m_StartPos;
UpdateVirtualAxes(m_StartPos);
}
}
}
Answer by InfiniteSoftware · Jul 12, 2016 at 08:06 AM
@Aissasa As I understand it, you adapted this to the Joystick Script found in the standard assets. I have a similar case. I used standard assets Joystick and my player is moving as I want him to, but now I want to add the same effect as you. But I don't get how you adapted this piece of code to the rest. Could you show us your whole code? or provide the new variables that you used, etc. Thanks in advance!
All the changes are made in the Joystick class. I'll post the final code of that class. All the changes I made are surrounded by a //abenz comment. I hope that helps :D If you don't get some methods tell and I'll explain as much as I can, It's been a while. Here's a link to the code: http://pastie.org/private/8eux7wwjwdwuptcmfvw1g
Your answer
Follow this Question
Related Questions
Unity Mobile Resolution Problems 2 Answers
Unity 5 mobile single stick control 6 Answers
Show Joystick on the left part of the screen only 1 Answer