Hi there, I got problems with the following script. It is an enemy script, like the one for Slender. It works fine, but when there is more than one page in the scene, the second or third can't be collected. I tried almost everything, but I couldn't find my mistake.
Thanks for help!
Here's the script:
#pragma strict
@script RequireComponent( AudioSource )
var pages : int = 0;
var pagesToWin : int = 8;
var distanceToPage : float = 2.5;
public var pagePickup : AudioClip;
var theEnemy : EnemyScript;
function Start()
{
Screen.lockCursor = true;
// find and store a reference to the enemy script (to reduce distance after each page is collected)
if ( theEnemy == null )
{
theEnemy = GameObject.Find( "Enemy" ).GetComponent( EnemyScript );
}
}
function Update()
{
//if ( Input.GetMouseButtonUp(0) ) // use E in editor as LockCursor breaks with left mouseclick
if ( Input.GetMouseButtonDown(0) || Input.GetKeyDown(KeyCode.E) )
{
//var ray = Camera.main.ScreenPointToRay( Input.mousePosition ); // always cast ray from center of screen
var ray = Camera.main.ScreenPointToRay( Vector3( Screen.width * 0.5, Screen.height * 0.5, 0.0 ) );
var hit : RaycastHit;
if ( Physics.Raycast( ray, hit, distanceToPage ) )
{
//if ( hit.collider.gameObject.name == "Page" )
if ( hit.collider.gameObject.tag == "Page" )
{
pages += 1;
//Debug.Log( "A pages was picked up. Total pages = " + pages );
audio.PlayClipAtPoint( pagePickup, transform.position );
Destroy( hit.collider.gameObject );
// make enemy follow closer
theEnemy.ReduceDistance();
}
}
}
}
function OnGUI()
{
if ( pages < pagesToWin )
{
GUI.Box( Rect( (Screen.width * 0.5) - 60, 10, 120, 25 ), "" + pages.ToString() + " Pages" );
}
else
{
GUI.Box( Rect( (Screen.width/2)-100, 10, 200, 35 ), "All Pages Collected!" );
// Application.LoadLevel( "sceneWin" );
}
}
↧