Search Unity

Slender Parody - Picking up pages doesn't work

Discussion in 'Scripting' started by sandwichmenno, Nov 26, 2012.

  1. sandwichmenno

    sandwichmenno

    Joined:
    Nov 26, 2012
    Posts:
    5
    Hello,

    I am making a Slender game. It's going well, but I am stuck with the pages.
    When you press E if you see a page the var papers will raise with 1, and the page disappears.
    But when I walk against the page, is it solid and you can't pick it up. The code I use is beneath.
    What's wrong?

    Code (csharp):
    1. #pragma strict
    2. @script RequireComponent( AudioSource )
    3.  
    4. var papers : int = 0;
    5. var papersToWin : int = 8;
    6. var distanceToPaper : float = 2.5;
    7.  
    8. public var paperPickup : AudioClip;
    9.  
    10. var theEnemy : EnemyScript;
    11.  
    12.  
    13.  
    14.  
    15. function Start()
    16. {
    17.     Screen.lockCursor = true;
    18.  
    19.     // find and store a reference to the enemy script (to reduce distance after each paper is collected)
    20.     if ( theEnemy == null )
    21.     {
    22.        theEnemy = GameObject.Find( "Enemy" ).GetComponent( EnemyScript );
    23.     }
    24. }
    25.  
    26.  
    27. function Update()
    28. {
    29.     //if ( Input.GetMouseButtonUp(0) ) // use E in editor as LockCursor breaks with left mouseclick
    30.     if ( Input.GetMouseButtonDown(0) || Input.GetKeyDown(KeyCode.E) )
    31.     {
    32.         //var ray = Camera.main.ScreenPointToRay( Input.mousePosition ); // always cast ray from center of screen
    33.         var ray = Camera.main.ScreenPointToRay( Vector3( Screen.width * 0.5, Screen.height * 0.5, 0.0 ) );
    34.         var hit : RaycastHit;
    35.         if ( Physics.Raycast( ray, hit, distanceToPaper ) )
    36.         {
    37.             //if ( hit.collider.gameObject.tag == "Paper" )
    38.             if ( hit.collider.gameObject.name == "Paper" )
    39.             {
    40.                 papers += 1;
    41.                 //Debug.Log( "A paper was picked up. Total papers = " + papers );
    42.  
    43.                 audio.PlayClipAtPoint( paperPickup, transform.position );
    44.  
    45.                 Destroy( hit.collider.gameObject );
    46.  
    47.                 // make enemy follow closer
    48.                 theEnemy.ReduceDistance();
    49.             }
    50.         }
    51.     }
    52. }
    53.  
    54.  
    55. function OnGUI()
    56. {
    57.     if ( papers < papersToWin )
    58.     {
    59.        GUI.Box( Rect( (Screen.width * 0.5) - 60, 10, 120, 25 ), "" + papers.ToString() + " Papers" );
    60.     }
    61.     else
    62.     {
    63.        GUI.Box( Rect( (Screen.width/2)-100, 10, 200, 35 ), "All Papers Collected!" );
    64.        // Application.LoadLevel( "sceneWin" );
    65.     }
    66. }
     
  2. Artaani

    Artaani

    Joined:
    Aug 5, 2012
    Posts:
    423
    Oh no... One another Slender game...
     
  3. Democre

    Democre

    Joined:
    Mar 31, 2010
    Posts:
    345
    It's not another Slender game, it's a parody!

    @sandwich, check out OnColllisionEnter and OnTriggerEnter.
     
  4. sandwichmenno

    sandwichmenno

    Joined:
    Nov 26, 2012
    Posts:
    5