Search Unity

Look and shoot rays

Discussion in 'Scripting' started by Draphonex, Jun 4, 2018.

  1. Draphonex

    Draphonex

    Joined:
    May 30, 2018
    Posts:
    1
    My script makes camera to turn and look around but when i try to tap and shoot ray cast to change some objects position by hit information other functions are stopping to i cant look around anymore i dont get how this touch functions work can anyone help me to tell my mistake thank you for your help

    Code (CSharp):
    1.    
    2.  
    3.     Vector2 startPos;
    4.     Vector2 lastPos;
    5.     bool touchStays = false;
    6.     bool touchMoves = false;
    7.     float turnleftRight;
    8.     float turnupDown;
    9.     public GameObject pos;
    10.     void Update()
    11.  
    12.     {
    13.  
    14.      
    15.  
    16.         if (lastPos.x > startPos.x && touchStays == false && touchMoves == true)
    17.         {
    18.             turnleftRight++;
    19.         }
    20.  
    21.         if (lastPos.x < startPos.x && touchStays == false && touchMoves == true)
    22.         {
    23.  
    24.             turnleftRight--;
    25.         }
    26.  
    27.         if (lastPos.y > startPos.y && touchStays == false && touchMoves == true)
    28.         {
    29.  
    30.             turnupDown++;
    31.         }
    32.  
    33.         if (lastPos.y < startPos.y && touchStays == false && touchMoves == true)
    34.         {
    35.  
    36.             turnupDown--;
    37.         }
    38.  
    39.         turnupDown = Mathf.Clamp(turnupDown, -90.0f, 90.0f);
    40.  
    41.         transform.rotation = Quaternion.Euler(-turnupDown, turnleftRight, 0);
    42.      
    43.         if (Input.touchCount > 0)
    44.         {
    45.            
    46.             Touch touch = Input.GetTouch(0);
    47.  
    48.             if(touch.tapCount>0)
    49.             {
    50.                 RaycastHit hit;
    51.                 Ray ray = Camera.main.ScreenPointToRay(touch.position);
    52.  
    53.                 if (Physics.Raycast(ray, out hit, Mathf.Infinity))
    54.                 {
    55.                     if (hit.transform.tag == "Ground")
    56.                     {
    57.                         pos.transform.position = hit.transform.position;
    58.  
    59.                     }
    60.                 }
    61.             }
    62.            
    63.             switch (touch.phase)
    64.             {
    65.  
    66.              
    67.                case TouchPhase.Began:
    68.                     startPos = touch.position;
    69.                     break;
    70.  
    71.                 case TouchPhase.Moved:
    72.                     touchStays = false;
    73.                     touchMoves = true;
    74.                     lastPos = touch.position;
    75.                     break;
    76.  
    77.                 case TouchPhase.Stationary:
    78.                     touchStays = true;
    79.                     touchMoves = false;
    80.                     break;
    81.  
    82.                 case TouchPhase.Ended:
    83.                     touchStays = false;
    84.                     touchMoves = false;
    85.                     break;
    86.             }
    87.         }
    88.     }
    89. }