- Home /
What's grong with this script?
#pragma strict
@script RequireComponent ( AudioSource )
var papers : int = 0;
var papersToWin = 5;
var distanceToPaper : float = 2.5;
public var paperPickup : AudioClip;
function Start ()
{
Screen.lockCursor = true;
}
function Update ()
{
if(Input.GetMouseButtonDown (0) || Input.GetKeyDown (KeyCode.E))
{
var ray = Camera.main.ScreenPointToRay(Vector3(Screen.width * 0.5, Screen.height * 0.5, 0.0));
var hit : RaycastHit;
if(Physics.Raycast(ray, hit, distanceToPaper))
{
if(hit.collider.gameObject.name == "Paper")
{
papers += 1;
audio.PlayClipAtPoint(paperPickup, transform.position);
Destroy(hit.collider.gameObject);
}
}
}
}
function OnGUI ()
{
if(papers < papersToWin)
{
GUI.Box(Rect((Screen.width * 0.5) -60, 10, 120, 25), " " + papers.ToString() + " papers");
}
else
GUI.Box(Rect((Screen.width/2) - 100, 10, 200, 35), "All Papers Collected!");
}
I dont know how to script, i am a designer, but my question is why when i picked up all the papers the game is still running? Thanks to google i find this script.
If you see "All Papers Collected" then your script works. There is no other End-Game code aside from this.
Its now formatted properly, mainly due to my trigger happiness with my new 2$$anonymous$$ powers.
Haha;) You "tagged" me in your script! Got an e$$anonymous$$ail. "@script RecuireComponent ( AudioSource )" But im tired and i can't really read it through now. Srry bro! Ill check on it tomorrow!
Answer by AlucardJay · Jun 24, 2014 at 08:50 PM
Nothing is grong with the script. I have no idea where you got this version of my script, but for the sake of answering and closing this question, you need to add your own solution for when all the papers are collected. It is clear in the GUI when all the papers are collected. Anyway, here is a more detailed version which clearly indicates what to do when the win condition has been reached :
#pragma strict
@script RequireComponent ( AudioSource )
var papers : int = 0;
var papersToWin = 5;
var distanceToPaper : float = 2.5;
public var paperPickup : AudioClip;
function Start ()
{
Screen.lockCursor = true;
}
function Update ()
{
if(Input.GetMouseButtonDown (0) || Input.GetKeyDown (KeyCode.E))
{
var ray = Camera.main.ScreenPointToRay(Vector3(Screen.width * 0.5, Screen.height * 0.5, 0.0));
var hit : RaycastHit;
if(Physics.Raycast(ray, hit, distanceToPaper))
{
if(hit.collider.gameObject.name == "Paper")
{
papers += 1;
audio.PlayClipAtPoint(paperPickup, transform.position);
Destroy(hit.collider.gameObject);
}
// check if all papers have been collected
if ( papers >= papersToWin )
{
Debug.Log( "All Papers Collected!" );
// ** ADD YOUR WIN CONDITION HERE **
// probably load a new scene, eg
// Application.LoadLevel( "WinScene" );
}
}
}
}
function OnGUI ()
{
if(papers < papersToWin)
{
GUI.Box(Rect((Screen.width * 0.5) -60, 10, 120, 25), " " + papers.ToString() + " papers");
}
else
GUI.Box(Rect((Screen.width/2) - 100, 10, 200, 35), "All Papers Collected!");
}
For further information, you can watch one of the many videos I have made on collecting papers (and I thought this had stopped haunting me...)
new guide full playlist with win and lose scenes near the end
Alucard, I wanted to ask you if you could take a look at the coding in my unity file, as the code I used from your code didn't work. The enemy only follows the player if the player has a direct line of sight with the player, and the player interrupts it, but comes back still looking at it. When the player does do that, the enemy slides towards the player and stops when they look away, then look back. If I do need to upload my unity file, keep in $$anonymous$$d it'll be big due to the assets I have.
Alucard thanks, yes i Heard something that you give up about the slender scripts, well thanks dude, the next time i will watch your videos.
Your answer
Follow this Question
Related Questions
How to get an object / AI to follow you? 1 Answer
Turning when not seen. 0 Answers
Slender type game 1 Answer
where is a good script to collect pages like in "slender"? 1 Answer
Slender type game need help 0 Answers