- Home /
How do I create an invisible joystick that appears anywhere on the iOS screen?
Hi, I'm creating a third person game and I want to make a single joystick controller but there are two distinctive things I want this joystick to do. I want it to be invisible until the player touches the screen. Then it becomes visible under the player's finger. I also want, and this is the hard part, to have the player be able to touch anywhere on the screen and have the joystick appear. I don't want it to be set in any particular location. Is there anywhere that I might be able to find this information or can anyone help me to create it? Thanks so much! Tom
Answer by MakeCodeNow · Jul 10, 2014 at 03:23 AM
Here's my version of the code that does that. Basic idea is wait for a touch to be down, use that as the center of the joystick, and then compute the joystick deflection as the difference between current finger pos and initial pos.
protected class TrackedTouch
{
public Vector2 startPos;
public Vector2 currentPos;
}
void _UpdateTouches()
{
int numTouches = Input.touchCount;
for(int touchIndex = 0; touchIndex < numTouches; ++touchIndex)
{
Touch touch = Input.touches[touchIndex];
if(touch.phase == TouchPhase.Began)
{
Debug.Log("Touch " + touch.fingerId + "Started at : " + touch.position);
TrackedTouch newTouch = new TrackedTouch();
newTouch.startPos = touch.position;
newTouch.currentPos = touch.position;
_touches.Add(touch.fingerId, newTouch);
}
else if(touch.phase == TouchPhase.Canceled || touch.phase == TouchPhase.Ended)
{
Debug.Log("Touch " + touch.fingerId + "Ended at : " + touch.position);
_touches.Remove(touch.fingerId);
}
else
{
TrackedTouch currentTouch;
if(_touches.TryGetValue(touch.fingerId, out currentTouch))
{
currentTouch.currentPos = touch.position;
}
}
}
}
protected Vector2 GetScreenJoystick(bool left)
{
foreach(TrackedTouch touch in _touches.Values)
{
float halfScreenWidth = Screen.width * 0.5f;
if((left && touch.startPos.x < halfScreenWidth) ||
(!left && touch.startPos.x > halfScreenWidth))
{
Vector2 screenJoy = touch.currentPos - touch.startPos;
screenJoy.x = Mathf.Clamp(screenJoy.x / (halfScreenWidth * 0.5f * TouchScreenLookScale), -1f, 1f);
screenJoy.y = Mathf.Clamp(screenJoy.y / (Screen.height * 0.5f * TouchScreenLookScale), -1f, 1f);
return screenJoy;
}
}
return Vector2.zero;
}
Thanks so much @$$anonymous$$akeCodeNow for your fast and detailed response. I'm assu$$anonymous$$g it works because I am used to Unity's JavaScript and am still rather new at that. C# is something that I haven't used at all yet. You have done so much with this that I am very reluctant to ask if it would be possible for you to put it in JavaScript? I'm at a loss because I don't know how to troubleshoot C#. When I pasted your code into a C# file, I received 3 error codes: 1) Assets/$$anonymous$$yScripts/InvisibleJoystickC.cs(1,17): error CS1527: Namespace elements cannot be explicitly declared as private, protected or protected internal: 2) Assets/$$anonymous$$yScripts/InvisibleJoystickC.cs(7,6): error CS0116: A namespace can only contain types and namespace declarations: and 3) Assets/$$anonymous$$yScripts/InvisibleJoystickC.cs(38,19): error CS0116: A namespace can only contain types and namespace declarations. I pasted it exactly as you have it above. I would really appreciate any further help you are willing to give me. Thanks so much for your time!!! $$anonymous$$
Yeah, that is code roughly extracted from a larger class. You'll need to do the work yourself if you want it ported to JavaScript. This is Unity Answers, not Unity Get Other People To Do Your Work For You.
Thanks for your help. I'll do my best. $$anonymous$$y game is 3/4 of the way done. This is all that's stopping me.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Constrain touch joystick around a point 0 Answers
How to use the joystick to control the character 3 Answers
Touching a collider? 1 Answer
Touch input firing twice on ios 2 Answers