Search Unity

Raycast Problems

Discussion in 'Scripting' started by slide, Apr 7, 2011.

  1. slide

    slide

    Joined:
    Feb 2, 2011
    Posts:
    72
    I'm using a ray cast on my menu screen to make an action when the screen is clicked. however the code is performing when the mouse hovers over it not when the mouse is clicked!

    Code (csharp):
    1. public class InterLevelScript : MonoBehaviour {
    2.    
    3.     public bool audioStop;
    4.     public GameObject arrow;
    5.     public GameObject nails;
    6.     public GameObject cement;
    7.  
    8.     // Use this for initialization
    9.     void Start () {
    10.    
    11.     }
    12.    
    13.     // Update is called once per frame
    14.     public void Update () {
    15.                 if(Application.loadedLevel==(1))
    16.         {
    17.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    18.             RaycastHit hit = new RaycastHit();
    19.             if (Physics.Raycast(ray, out hit))
    20.             {
    21.                
    22.  
    23.             if (hit.collider.gameObject.name=="Arrow")
    24.             {
    25.                 arrow.active=false;
    26.                 nails.active=true;
    27.             }
    28.             if (hit.collider.gameObject.name=="Nails")
    29.             {
    30.                 nails.active=false;
    31.                 cement.active=true;
    32.             }
    33.             if (hit.collider.gameObject.name=="Cement")
    34.             {
    35.                 Application.LoadLevel(2);
    36.             }
    37.         }
    38.        
    39.             }
    40.     if(audioStop==true)
    41.         {
    42.             AudioListener.pause = true;        
    43.         }
    44.         if(audioStop==false)
    45.         {
    46.            
    47.             AudioListener.pause = false;    
    48.         }
    49.  
    50.  
    51. }
    52. }
    53.  
     
  2. Marrrk

    Marrrk

    Joined:
    Mar 21, 2011
    Posts:
    1,032
  3. slide

    slide

    Joined:
    Feb 2, 2011
    Posts:
    72
    ahhh of course xd cheers!