Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using devm.input;
- [RequireComponent(typeof(Collider))]
- public class JoyStick : MonoBehaviour {
- public float AngleMul = 5f;
- public float AngleLimits = 80f;
- TouchInput touchInput;
- Camera guiCamera;
- new Collider collider;
- float angle;
- int fingerId;
- Vector3 fingerDownAt;
- public float Power{
- get{
- float pow = angle / AngleLimits;
- if(Mathf.Abs(pow) < 0.25f)
- pow = 0f;
- return pow;
- }
- }
- // Use this for initialization
- void Start () {
- touchInput = Dependencies.Get<TouchInput>();
- guiCamera = GetComponentInParent<Camera>();
- collider = GetComponent<Collider>();
- angle = 0f;
- fingerId = -1;
- }
- // Update is called once per frame
- void Update () {
- for(int i=0; i < touchInput.TouchCount; i++){
- TouchInput.TouchPoint touch = touchInput.Touches[i];
- if(touch.Phase == TouchPhase.Began)
- {
- Ray ray = guiCamera.ScreenPointToRay(touch.Position);
- RaycastHit hitinfo;
- if(collider.Raycast(ray, out hitinfo, 100f))
- {
- fingerId = touch.FingerId;
- fingerDownAt = touch.Position;
- }
- }
- else if(touch.FingerId == fingerId)
- {
- Vector3 fingerPos = touch.Position;
- Vector3 fingerDelta = fingerPos - fingerDownAt;
- angle = fingerDelta.y / (float)Screen.height;
- angle *= AngleMul;
- angle = Mathf.Clamp(angle, -AngleLimits, AngleLimits);
- }
- if(touch.Phase == TouchPhase.Ended && touch.FingerId == fingerId)
- fingerId = -1;
- }
- if(fingerId == -1)
- {
- //ease back
- angle *= 0.75f;
- }
- transform.localRotation = Quaternion.Euler(angle, 0f, 0f);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement