- Home /
How do you get Virtual Joysticks to work?
Hi
I know these questions pop up now and then and its all the same stuff but I just don't understand everything that is said in the other answers however, I think I have got an idea on how its supposed to work.
I make my character move by this code:
Vector3 input = new Vector3(Input.GetAxisRaw("Horizontal"),0, Input.GetAxisRaw("Vertical"));
if (input != Vector3.zero) //fingers taken off controls, player doesnt reset to defaul position
{
targetRotation = Quaternion.LookRotation(input);
transform.eulerAngles = Vector3.up * Mathf.MoveTowardsAngle(transform.eulerAngles.y, targetRotation.eulerAngles.y, rotationSpeed * Time.deltaTime);
}
Vector3 motion = input;
motion *= (Mathf.Abs (input.x) == 1 && Mathf.Abs (input.z) == 1) ? .7f : 1;
rigidbody.velocity = motion * currentSpeed;
I have the Standard Mobile Assets (Mobile) and have dragged the Single Joystick into my scene.
I get that I have to extract the joyststick position values (which I believe to be 1, 0, -1 etc)? I have tried to follow this forum which hasn't worked out. Here it says I want to add public Joystick jstick to my player script and then drag the joystick object into that variable in the inspector. For a start it won't let me enter in public Joystick.
Even if I got that to work, I wouldn't know how to use the values with my code. Just can't get my head round it at all.
Can anyone help please. Totally lost.
Thanks in advance
Answer by tanoshimi · Nov 24, 2014 at 09:13 PM
Scroll down this tutorial to the bit where he adds the joystick functionality: http://www.raywenderlich.com/25349/how-to-get-started-with-unity-part-23