0

In desktop version, my character's arm is rotates according to the movement of the mouse. I need the same thing in mobile version but this time I need a joystick to rotate his arm. How can I do that?

enter image description here

 //Here is my code, for rotating arm
 void Update()
{
    float horizontal = Input.GetAxis("Joy Y") * Time.deltaTime;
    float vertical = Input.GetAxis("Joy X") * Time.deltaTime;
    float rotZ = Mathf.Atan2(horizontal, vertical) * Mathf.Rad2Deg;
    transform.rotation = Quaternion.Euler(0f, 0f, rotZ); 
}

I have changed the code a bit, for now player arm is rotating but, this time there is another problem. Arm is doing unexpected movements when I try to move with joystick I think joystick is mixed with mouse. I don't know how to seperate it?

komtan
  • 81
  • 1
  • 12
  • This is really too broad for a question on Stack Overflow. At the very least, you should read some tutorials on creating that kind of mobile control, and try to implement it yourself. If you get stuck and can't find a solution despite your best efforts, well, that's when you should be posing a question on this site. – Serlite May 25 '16 at 17:06
  • @Serlite I am really wondering, how did you understand that I didn't try any solution :) and by the way you should ask specific questions if there is one place you didn't understand in my question. – komtan May 25 '16 at 18:12
  • Unfortunately, you haven't supplied evidence of previous attempts in your original question. If you have made efforts to tackle this problem on your own, please supply your best attempt in a [mcve]. Then, explain what you intended for your code to do and how it fails to meet those requirements. Your question is understandable, but currently lacks these sorts of details that make it specific enough to be answerable in Stack Overflow's Q/A format. – Serlite May 25 '16 at 18:30

1 Answers1

1

You make a JoyStick then replace the mouse code with JoyStick code.

Unity have Assets called CrossPlatformInputManager that can do that. You have to import it and modify it a little bit in order to use it. Watch this to understand how to import it.

Now, you can replace your Input.GetAxis and Input.GetAxisRaw functions with CrossPlatformInputManager.GetAxis("Horizontal") and CrossPlatformInputManager.GetAxisRaw("Horizontal").

If you get it to work, then can use below to make your code compatible with both mobile and desktop.

#if UNITY_EDITOR || UNITY_STANDALONE || UNITY_WEBGL
//put your Input.GetAxis` and `Input.GetAxisRaw` code here
#elif UNITY_ANDROID || UNITY_IOS 
//Put your `CrossPlatformInputManager.GetAxis("Horizontal")` and `CrossPlatformInputManager.GetAxisRaw("Horizontal")`. here
#endif
Programmer
  • 121,791
  • 22
  • 236
  • 328
  • Firstly thank you very much for answering my question, I have imported the joystick stuff as you said, but unfortunately I didn't get it, where should I replace and change it. So could you help more please :) @Programmer – komtan May 26 '16 at 09:17
  • @komtan I really can't help any further because I don't don't what your code looks like. You have to post the code that gets the input from mouse and control the 2D Object then I will help translate it directly. Otherwise I don't know what your script looks like. – Programmer May 26 '16 at 09:23
  • Actually not really but, it gave me an idea @Programmer – komtan May 28 '16 at 11:38