Advertisement
Guest User

Untitled

a guest
Aug 27th, 2015
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.72 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using devm.input;
  4.  
  5. [RequireComponent(typeof(Collider))]
  6. public class JoyStick : MonoBehaviour {
  7.  
  8.     public float AngleMul = 5f;
  9.     public float AngleLimits = 80f;
  10.  
  11.     TouchInput touchInput;
  12.     Camera guiCamera;
  13.     new Collider collider;
  14.  
  15.     float angle;
  16.     int fingerId;
  17.     Vector3 fingerDownAt;
  18.    
  19.     public float Power{
  20.         get{
  21.             float pow = angle / AngleLimits;
  22.            
  23.             if(Mathf.Abs(pow) < 0.25f)
  24.                 pow = 0f;
  25.            
  26.             return pow;
  27.         }
  28.     }
  29.  
  30.  
  31.     // Use this for initialization
  32.     void Start () {
  33.        
  34.         touchInput = Dependencies.Get<TouchInput>();
  35.         guiCamera = GetComponentInParent<Camera>();
  36.         collider  = GetComponent<Collider>();
  37.        
  38.         angle = 0f;
  39.         fingerId = -1;
  40.     }
  41.    
  42.     // Update is called once per frame
  43.     void Update () {
  44.        
  45.         for(int i=0; i < touchInput.TouchCount; i++){
  46.            
  47.             TouchInput.TouchPoint touch = touchInput.Touches[i];
  48.            
  49.        
  50.             if(touch.Phase == TouchPhase.Began)
  51.             {
  52.                
  53.                 Ray ray = guiCamera.ScreenPointToRay(touch.Position);
  54.                 RaycastHit hitinfo;
  55.                
  56.                 if(collider.Raycast(ray, out hitinfo, 100f))
  57.                 {
  58.                     fingerId = touch.FingerId;
  59.                     fingerDownAt = touch.Position;
  60.                 }              
  61.                
  62.             }  
  63.             else if(touch.FingerId == fingerId)
  64.             {
  65.                
  66.                 Vector3 fingerPos = touch.Position;
  67.                
  68.                 Vector3 fingerDelta = fingerPos - fingerDownAt;
  69.                
  70.                 angle = fingerDelta.y / (float)Screen.height;
  71.                 angle *= AngleMul; 
  72.                 angle = Mathf.Clamp(angle, -AngleLimits, AngleLimits);         
  73.             }
  74.                    
  75.             if(touch.Phase == TouchPhase.Ended && touch.FingerId == fingerId)
  76.                 fingerId = -1;
  77.         }
  78.        
  79.         if(fingerId == -1)
  80.         {
  81.             //ease back
  82.            
  83.             angle *= 0.75f;
  84.         }
  85.        
  86.         transform.localRotation = Quaternion.Euler(angle, 0f, 0f);
  87.        
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement