Enemy Script (84892)

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();

I 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.

The 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..

Ah, 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.

Your script works fine with me, sure you didn't forget to tag your other page? hehe

1 Answer

1

Why don’t you write a script that you place on each paper inside the game? This way you can simply use

function OnMouseover(){
//Create a static var inside your main script, example: static var PageCounter:int=0;
  if ( Input.GetMouseButtonDown(0) || Input.GetKeyDown(KeyCode.E) ) {
YourScript.PageCounter+=1
}
}

this way you can check per page how many you have picked up by simply adding this script to every paper.

Unity doesn't let me get into playmode. There are compiler errors. Hm, that's annoying. But your solution seems like it could work.

try to check if you haven't forgotten any semicolons or brackets. also this script above is made on the fly it might need some tweaking as I have not checked in Unit (I was on school at the moment of writing).

I'm very new in javascripting. Would it take too much time for you, to look for these little mistakes? It would be a great help to me! Thanks for your efforts :)

i'm having class this week, Next week I might find some time to check it out. Is that the complete script up there, just in case I'l save it down to a file. You will receive a comment if I find a solution. If not I will still mention it here so you won't wait for no reason.

Yes it's the whole script. Thanks, that you try to solve my problem :)