Search Unity

Convert TouchPhase.Moved to be used with mouse input

Discussion in 'Scripting' started by hizral, May 3, 2015.

  1. hizral

    hizral

    Joined:
    Apr 27, 2010
    Posts:
    568
    Hello there..

    I have make this script for my game. This script I made is to be used on mobile devices. But I need to change this script so I can used is on pc with mouse as well. So I'm am getting a bit of trouble with changing the script so it can used mouse input. What should I do to change the script so it will work with mouse.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ControlMouse : MonoBehaviour {
    5.    
    6.     public GameObject player1;
    7.     public bool typeTouch;
    8.    
    9.     public float moveSpeed;
    10.     private float moveX;
    11.     private float moveY;
    12.    
    13.     private Vector3 move;
    14.    
    15.     public Touch touch;
    16.    
    17.     public Camera theKamera;
    18.     private RaycastHit hit;
    19.      private Ray ray;
    20.    
    21.     public HoverManager hover;
    22.    
    23.     private Vector2 direction;
    24.    
    25.     // Use this for initialization
    26.     void Start ()
    27.     {
    28.         typeTouch = false;
    29.         player1 = GameObject.FindWithTag ("fighterOne");
    30.        
    31.         ScreenLock ();
    32.     }
    33.    
    34.     // Update is called once per frame
    35.     void Update ()
    36.     {
    37.         if (typeTouch)
    38.         {  
    39.             MouseOn ();
    40.         }
    41.     }
    42.    
    43.     void MouseOn ()
    44.     {
    45.         ray = theKamera.ScreenPointToRay (Input.mousePosition);
    46.        
    47.         if (Input.GetMouseButton (0))
    48.         {
    49.             if (Physics.Raycast (ray, out hit, 100))
    50.             {
    51.                 if (hit.collider.tag == "player1Control" && Input.touchCount == 1)
    52.                    {
    53.                     touch = Input.GetTouch (0);
    54.                        
    55.                     if (touch.phase == TouchPhase.Moved)
    56.                     {
    57.                         moveX = moveSpeed * touch.deltaPosition.x;
    58.                         moveY = moveSpeed * touch.deltaPosition.y;
    59.                            
    60.                         move = new Vector3 (moveX, moveY, 0.0f);
    61.                         player1.transform.position += move * Time.deltaTime;
    62.                     }
    63.                    
    64.                     if (touch.deltaPosition.x > 0)
    65.                     {
    66.                         hover.front = true;
    67.                     }
    68.                     else if (touch.deltaPosition.x < 0)
    69.                     {
    70.                         hover.back = true;
    71.                     }
    72.                     else
    73.                     {
    74.                         hover.front = false;
    75.                         hover.back = false;
    76.                     }
    77.                    
    78.                     if (touch.deltaPosition.y > 0)
    79.                     {
    80.                         hover.up = true;
    81.                     }
    82.                     else if (touch.deltaPosition.y < 0)
    83.                     {
    84.                         hover.down = true;
    85.                     }
    86.                     else
    87.                     {
    88.                         hover.up = false;
    89.                         hover.down = false;
    90.                     }
    91.                 }
    92.             }
    93.         }
    94.     }
    95.    
    96.     void ScreenLock ()
    97.     {      
    98.         direction = player1.transform.position;
    99.        
    100.         if (direction.x >= 7f)
    101.         {
    102.             direction.x = 7f;
    103.             player1.transform.position = direction;
    104.         }
    105.         else if ( direction.x <= -7)
    106.         {
    107.             direction.x = -7f;
    108.             player1.transform.position = direction;
    109.         }
    110.        
    111.         if (direction.y >= 5.7f)
    112.         {
    113.             direction.y = 5.7f;
    114.             player1.transform.position = direction;
    115.         }
    116.         else if (direction.y <= -5.0f)
    117.         {
    118.             direction.y = -5.0f;
    119.             player1.transform.position = direction;          
    120.         }
    121.     }
    122. }

    Thank you.
     
  2. hizral

    hizral

    Joined:
    Apr 27, 2010
    Posts:
    568
    Bump