- Home /
Picking Up Pages- Slender Like
Hey Guys this is my first ever question but can i have some help? I dont know if the script came out right. I made it so you can edit this post to make the script right
I am making my own slender game, and i want to pick up pages - I got off another tutorial to use this script and attach to the player:
#pragma strict
var papers : int = 0;
var papersToWin : int = 8;
var distanceToPaper : float = 2.5;
//public var paperpickup : AudioClip;
function Start()
{
Screen.lockCursor = true;
}
function Update()
{
if ( Input.GetMouseButtonUp(0) )
{
var ray = Camera.main.ScreenPointToRay( Input.mousePosition );
var hit : RaycastHit;
if ( Physics.Raycast( ray, hit, distanceToPaper ) )
{
if ( hit.collider.gameObject.name == "Paper" )
{
papers += 1;
//audio.PlayOneShot(paperpickup);
Debug.Log( "A paper was picked up. Total papers = " + papers );
Destroy( hit.collider.gameObject );
}
}
}
}
function OnGUI()
{
if ( papers < papersToWin )
{
GUI.Box( Rect( (Screen.width/2)-100, 10, 200, 35 ), "" + papers + " Papers" );
}
else
{
GUI.Box( Rect( (Screen.width/2)-100, 10, 200, 35 ), "All Papers Collected!" );
}
}
I dont know if that is all correct or i need to edit something. The script works fine but i cant pick up pages. I have created a game object - Cube and edited it to look like a page.
Do i need to add a picking up script to the page? Anyone out there please help! I need this to work
Hi =]
1/ for further help, please format your code. You can do this by highlighting all your code, then clicking the 10101 button at the top of the edit window.
2/ I wrote that tutorial. If you read the code, it says
if ( hit.collider.gameObject.name == "Paper" ) {
so, make sure your cube has been renamed to Paper .
Just above this line, add the line :
Debug.Log( "Raycast hit " + hit.collider.gameObject.name );
this will then print in the console the name of the object that the raycast hit. if it is returning Cube, or anything that isn't Paper then it won't work. Here's the link to the full guide with instructions (I'm $$anonymous$$ $$anonymous$$ay) : http://answers.unity3d.com/questions/321749/how-do-you-pick-up-pages-like-in-slender.html
How did you go? There is a chance that you are not close enough to the paper. The value of distanceToPaper is currently 2.5 units, make this about 4 or 5 (so you can see the paper but not picking it up from the other side of the forest!). I saw you posted then deleted a comment. Still do the check as in my last comment, this will show if and what the raycast is hitting :
if ( Physics.Raycast( ray, hit, distanceToPaper ) )
{
Debug.Log( "Raycast hit " + hit.collider.gameObject.name );
if ( hit.collider.gameObject.name == "Paper" )
{
I didnt go so well, it isnt working at all :/ can you like just put a full script with everything in it?
Pleease i will post a screenshot of my Unity page - Its got the Paper and the script in my First Person Controller( I changed it to Player like you said in the Tutorial - But this was a while ago)
I found out the problem people are having with pick-up-papers, it is when running in the editor and using Screen.lockCursor = true; , as soon as the mouse is clicked, the lock is broken, then the ray casts from the mouse pointer ins$$anonymous$$d of from the center of the screen. This can be worked around by changing 2 lines. On the collect paper script, add the E key so you don't use the mouse in the editor :
if ( Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E) || Input.Get$$anonymous$$ouseButtonUp(0) )
the second line to change is straight after that one :
var ray = Camera.main.ScreenPointToRay( Vector3(Screen.width * 0.5, Screen.height * 0.5, 0) );
so the ray is cast from the center of the screen when the cursor is not locked. There seems to be interest in this so I'm working on a part 2 now, with fixes for people having problems with part 1 =]
here is a package of the project :
http://www.alucardj.net16.net/unityanswers/SlenderGuideV1-scene0.unitypackage
create a new project, then import the above asset. Open the folder _Scenes and then open scene0. Use the E key in the editor to pick up papers so that ScreenLock doesn't break, but hopefully adding the edits to the pick-up-paper script will make it work if ScreenLock breaks =]
Answer by AlucardJay · Nov 10, 2012 at 01:20 PM
This Answer is now CLOSED
Attention Slender Script Shoppers, this question will be closing in 2 minutes.
All future questions about my scripts will only be answered on the new forum page.
Here is the link you are looking for
Unity Forum : http://forum.unity3d.com/threads/172415-Slender-Guide-by-alucardj
I shall not be responding to any more posts here or any other question on Unity Answers either. All future traffic to my 'Slender' Guide is going to be solely through the forum page. Any questions that appear on Unity Answers will be closed and redirected to the forum page.
If you post a new question on Unity Answers with my scripts, I shall bring the hammer down !
While this may seem a little unfair, it is actually a good thing for all of your benefit. Every comment, change and update will appear there, for everyone to read and contribute to. No more searching through different questions just to get different parts of functionality, but everything you could need for starting your own horror survival game will be in one place. Doesn't that sound convenient ?!
See You there =]
Slender Guide Version 2
While I have finished writing the code, I don't have time to write a full guide explaining the steps taken and what I have coded in. I am changing the code in the below guide so people can still make their own rather than just using my package, and I really hope people write their own instead of just using my package.
Slender Guide Version 2 (I can only upload 5MB, so the project is very basic, but working!) : http://www.alucardj.net16.net/unityanswers/SlenderGuideV2-scene0.unitypackage
Fixed problems from Version 1 :
picking up papers in the editor - there were some issues with using LockCursor while running in the editor, hopefully these have been compensated for, recommend use the E key to pick up papers while running in the editor (but mouse still works). some movement issues - I had no problem, but it was brought up so I have tweaked the movement script.
New features :
The more papers picked up, the Man follows closer. Audio : when picking up paper, when the Man is first sighted, running and walking footsteps with adjustable audio gap between steps. Custom mesh with fading alpha fullscreen static effect.
And don't forget the awesome original flickering flashlight!
Here is the video link for the obstacle avoidance raycast : http://vimeo.com/9304844
Comments and feedback are welcome, as long as you have something more to say than just it doesn't work. The package proves it does. And please don't ask new questions, post comments here. If this keeps going or more people have requests, I might have to start a Unity Forum page.
Enjoy =]
How to make a 'Slender' game by Jay Kay (alucardj)
1- Create the terrain, and add a first person controller
there are many tutorials on both these topics, here are some quick links :
video : http://cgcookie.com/unity/2011/12/05/introduction-to-character-controllers/
written : http://wiki.unity3d.com/index.php?title=Terrain_tutorial
Some assets to help you out :
http://unity3d.com/support/resources/assets/terrain-assets
http://u3d.as/content/stormwater/heightfield-pack/2Q4
http://unity3d.com/support/resources/unity-extensions/terrain-toolkit
Also find some buildings (old shed, abandoned shack, etc) and anything you want to help the player use as 'landmarks' while searching around the terrain.
When you add a First Person Character Controller, Change the name of First Person Controller to Player.
2- Set the Ambience and Add a Flashlight
Here I am assuming you have created a terrain, added trees grass and buildings, and dropped in a first person character controller. Hopefully you have also pressed play and walked around the world you have created.
To give the scene a more 'spooky' feel, set the render settings.
In Unity, navigate to Edit > Render Settings
Enable (tick) the box that says Fog.
Click the Fog Colour box, set the ambient light to 29 on each of the RGB sliders. You can see the change in the scene, pick a colour that is dark but not pitch-black.
Click the Ambient Light Colour box, set the ambient light to 51 on each of the RGB sliders. You can see the change in the scene. Again, pick a colour that is dark but not pitch-black.
For a skybox I used the Moonshine skybox from the Standard Assets. (Assets > Import Package > Skyboxes). Drop Moonshine Skybox into Skybox Material (while in the Render Settings).
Now for the flashlight. Create a SpotLight, make it a child of the camera (so wherever the camera looks, the spotlight points). Set its Transform to X = 0; Y = 0.467; Z = 0; leave Rotations at 0, Scale at 1.
My settings for the spotlight are :
Range = 25; Spot Angle = 71.5870; Color I made very slightly yellow; Intensity = 1.85;
Now when you press play, you should be walking around your dark scene with a torchlight effect in front of where you look.
Advancing : I helped someone with a flickering flashlight, and have adapted that script to have the following behaviour. 1- light works for 1-2 minutes. 2- Light starts to flicker. 3- press F to reset light (light flashes twice as if being tapped/shaken) then light works again for another 1-2 minutes. I think it works well to add suspense (the flashlight suddenly flickers, and you have to reset it) . The link is : http://answers.unity3d.com/questions/316189/i-want-to-turn-my-flashlight-on-and-off-while-stil.html#answer-316302
3- How to Pick Up Papers
This one gets asked alot, and the usual response is a trigger-collider setup. For this tutorial, I am going to use a raycast method.
First create some papers. This part is easy =] Create a cube, scale it to X = 0.6; Y = 0.9; Z = 0.01;
Name it Paper. Now Duplicate it 7 times, so you have 8 Paper cubes total.
There are some images that can be found on page 5 of this thread : http://forum.unity3d.com/threads/134862-Slender-Man-Design-Outline
Create a material for each Paper, then apply them to the cubes. You can place these where you want around the scene, just make sure they are set at a realistic height, and preferably so they can only seen from one side (nailed to a thick tree, on the side of a shed).
Now to make it so these papers can be collected.
Create a new script, call it CollectPapers. Attach it to the Player. Here is the code :
function Start()
{
Go TO THE FORUM PAGE !
}
NOTE : in the Start function, there is the line Screen.lockCursor = true; this is to hide the cursor and lock it to the center of the screen. This can have some silly effects when you run in the editor and then click the mouse, but in a build you notice it works as expected. (this script is adapted from my answer here : http://answers.unity3d.com/questions/239927/Collecting-Papers-and-Keeping-Track-of-how-many-collected--need-help.html)
So, when the left mouse button is released, a ray is cast for a certain distance. If the player is close enough to something named Paper, and the ray hits Paper, then the count is increased and the Paper is destroyed.
At this stage you should be walking around your 'spooky' scene, collecting papers with the amount collected displayed at the top.
Advancing : I found this link for when the paper is found, first displaying the paper full-screen, then destroying the paper. I think that effect would be cool but havn't looked into it yet. The link is : http://answers.unity3d.com/questions/15438/trying-to-pick-up-and-see-paper-pop-up-gui-window.html
4- How to Have an Enemy Follow the Player at a Distance
If you were observant, you would have noticed a model where you got the images for the papers! You can use this, but as the 'man' always looks at the player, a 2D image can be used. Search for an image you want to use (), then make a material for that image. (http://forums.d2jsp.org/topic.php?t=64345908&f=202, http://www.zerochan.net/1271218, etc)
Create a Cube, name it Enemy, scale it to X = 1; Y = 2; Z = 0.1;
Remove or replace the Box Collider with a Capsule Collider ( Component > Physics > Capsule Collider ). My settings for the Capsule Collider are Center = 0 for XYZ, Radius = 0.5, Height = 1, Direction = Y-Axis.
Now also attach a Rigidbody Component ( Component > Physics > Rigidbody ). My settings for the Rigidbody are Mass = 1; Use Gravity = true.
Now for the script, create and call it EnemyScript. I have seen many answers use the Render.isVisible method. For this I am using a Dot.Product method. First there is a check if in a Dot.Product greater than 0.8, then check if within the maximum viewable distance, then check if there is a direct line-of-sight to the player (i.e. not occluded/behind a tree or building). Here is the code :
function Start()
{
Go TO THE FORUM PAGE !
}
5- Play and Test your Game =]
If all has gone well, you now have your very own Slender game !
This is just the start, try adding many more things :
Add Sounds !! Batteries to pick up for the Flashlight when it runs out of charge. Full screen display of the page when collected. A Game Over screen and a way to restart the game.
Hope you have found this useful and helpful =]
New Forum Thread. This is now the only place I shall write answers to problems with my scripts : http://forum.unity3d.com/threads/172415-Slender-Guide-by-alucardj