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" );
}
}
how many this distance is reduced? theEnemy.ReduceDistance();
– GjallanhornI think it reduces the distance to 10. But this works completely fine. The problem is the collecting of the pages. In my scene there are two pages at the moment. The script requires a gameobject with the tag "Page" and the name of the object "Page". But only one of the pages is collectable.
– TimNick151297The distance is part of this... you start the distance with 2.5, if this reduce to a very small or a negative number, you will never get true on your raycast. Try it without theEnemy.ReduceDistance() function..
– GjallanhornAh, now I understand. You thougt, theEnemy.ReduceDistance() function is important for the page. This function is linked with another script. It's the "Hunter" script. The distance 2.5 won't be reduced. This reduce thing is only important for Slender. He will get closer to the player then.
– TimNick151297Your script works fine with me, sure you didn't forget to tag your other page? hehe
– Gjallanhorn