Search Unity

Continuing action after raycast is not hitting object anymore

Discussion in 'Scripting' started by matias-e, Mar 5, 2015.

  1. matias-e

    matias-e

    Joined:
    Sep 29, 2014
    Posts:
    106
    Hey, so I'm trying to make a script with which I can rotate a gameobject with my mouse even after the ray is not hitting the inital object anymore. In essence I'm trying to select an object to rotate and not have the rotation stop even if I drag my cursor out of the objects bounds. Here's the script I'm using at the moment, which makes Unity crash:

    Code (CSharp):
    1. void Update () {
    2.        
    3.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    4.         RaycastHit hitInfo;
    5.    
    6.         if(Input.GetMouseButtonDown(0))
    7.         {
    8.             if(Physics.Raycast(ray, out hitInfo))
    9.             {
    10.                 if(hitInfo.collider.gameObject.tag == "rotatable")
    11.                 {
    12.                     activeRotation();
    13.                 }
    14.             }
    15.         }
    16.     }
    17.  
    18.     void activeRotation()
    19.     {
    20.         float spinX = Input.GetAxis("Mouse X");
    21.         float spinY = Input.GetAxis("Mouse Y");
    22.  
    23.         while(Input.GetMouseButton(0))
    24.         {
    25.         gameObject.transform.Rotate(spinX, 0, spinY);
    26.         }
    27.     }
    How could I implement this feature?
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,406
    Quick modifications (not tested..)

    Code (CSharp):
    1.    
    2.  
    3. Transform target;
    4.  
    5.  
    6. void Update () {
    7.          
    8.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    9.             RaycastHit hitInfo;
    10.      
    11.             if(!target && Input.GetMouseButtonDown(0))
    12.             {
    13.                 if(Physics.Raycast(ray, out hitInfo))
    14.                 {
    15.                     if(hitInfo.collider.gameObject.tag == "rotatable")
    16.                     {
    17.                         target = hit.transform;
    18.                         //activeRotation();
    19.                     }
    20.                 }
    21.             }
    22.  
    23.             if (target) activeRotation();
    24.  
    25.             if(target && Input.GetMouseButtonUp(0)) target=null;
    26.  
    27.         }
    28.    
    29.         void activeRotation()
    30.         {
    31.             float spinX = Input.GetAxis("Mouse X");
    32.             float spinY = Input.GetAxis("Mouse Y");
    33.    
    34. //            while(Input.GetMouseButton(0))
    35. //            {
    36.             target.Rotate(spinX, 0, spinY);
    37. //            }
    38.         }
    39.  
     
  3. matias-e

    matias-e

    Joined:
    Sep 29, 2014
    Posts:
    106
    Hey, that works! Thanks! That's a better option than what I figured out after posting. I did a bit of a hack where the rotation worked by instantiating an invisible mega collider (which covers the whole screen) at the position of the rotatable object, then making that the parent of the rotatable object and detaching + destroying it on mouse up.

    What does the 'if (target) activeRotation();' part basically mean?
     
  4. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,406
    its basically same as checking if the target is not assigned:
    Code (CSharp):
    1. if (target!=null) activeRotation();
     
  5. matias-e

    matias-e

    Joined:
    Sep 29, 2014
    Posts:
    106
    Cool, I think I get it now!