Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class Joystick : MonoBehaviour {
- public Touch joystickTouch;
- public bool isUsed;
- public bool stopUse;
- public GameObject myJoystick;
- public GameObject myInput;
- Vector2 joystickPos;
- Vector2 inputPos;
- private float joystickRadius = 40.0f;
- private Vector3 direction;
- private float speed;
- // Use this for initialization
- void Start () {
- myInput.transform.position = myJoystick.transform.position;
- joystickPos = myJoystick.transform.position;
- isUsed = false;
- stopUse = true;
- }
- // Update is called once per frame
- void Update ()
- {
- if(isUsed)
- {
- if (joystickTouch.phase == TouchPhase.Moved)
- {
- inputPos = joystickTouch.position;
- Vector2 dMove = inputPos - joystickPos;
- float angle = Mathf.Atan2(dMove.y,dMove.x);
- if (Vector2.Distance(joystickPos,inputPos)<=joystickRadius)
- myInput.transform.position = inputPos;
- else{
- dMove.x = Mathf.Cos(angle) * joystickRadius;
- dMove.y= Mathf.Sin(angle) * joystickRadius;
- myInput.transform.position = joystickPos + new Vector2(dMove.x,dMove.y);
- }
- }
- if (joystickTouch.phase == TouchPhase.Ended)
- {
- myInput.transform.position = myJoystick.transform.position;
- if(stopUse)
- {
- isUsed = false;
- stopUse = false;
- }
- }
- speed = Vector2.Distance(myJoystick.transform.position,myInput.transform.position)/8;
- if(speed>0)
- {
- direction = myInput.transform.position - myJoystick.transform.position;
- direction.z = direction.y;
- direction.y = 0.0f;
- }
- }
- }
- public Vector3 getDirection()
- {
- return direction;
- }
- public float getSpeed()
- {
- return speed;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement