- Home /
How to make a "Pick Up" script?
I would like to have a script that will be able to obviously pick up objects that are able to. More specifically, how can I pull off a paper note or letter. When you click 'e' on a object, a 'paper note' gui will appear on the screen, until the player closes it. How can I do this, and how can I do this neatly and efficiently? It's inspired by the game "Amnesia: The Dark Descent".
Answer by Cherno · Oct 13, 2013 at 11:44 PM
Put this script onto your player object or camera:
var Letter : GameObject;//leave empty, the script will assign it
var Image : Texture2D;//leave empty, the script will assign it
var ShowLetter : boolean;//don't touch :)
var LetterMask : LayerMask;//select the layer of your letter object in this drop-down list
var cam : Transform = Camera.main.transform;
function Update()
{
var ray = Ray(cam.position, cam.forward);
var hit : RaycastHit;//stores information about things we hit with our raycast
if(Input.GetKeyDown(KeyCode.E))
{
if(ShowLetter == false)
{
if (Physics.Raycast (ray, hit, Mathf.Infinity, LetterMask))
{
Letter = hit.collider.gameObject;//assigns the letter object that was hit by the raycast to this script's Letter variable so we can access it, should we want to pick it up.
Image = hit.collider.gameObject.GetComponent(LetterScript).LetterImage;//assign the LetterImage from the letter object that was hit to this script's Image variable so it can be accessed in OnGUI()
ShowLetter = true;
}
}
else
{
ShowLetter = false;//if the letter image is already being shown, hide it again.
//here, you could also make the player pick up the letter object by making it a child of himself:
if(Letter != null)//just to be sure, check if the Letter variable isn't empty
{
Letter.transform.parent = transform;//make the letter a child of the player so it stays with him. It's not really needed though.
Letter.transform.position = transform.position;//put the letter at our player's location.
Letter.renderer.enabled = false;//make to Letter object invisible, we don'T want to see it while carrying it, right?
Letter.collider.enabled = false;//disable the Letter object's collider, so it doesn't interfere with any raycasting we do.
}
}
}
}
function OnGUI()
{
if(ShowLetter == true)
{
GUI.DrawTexture (Rect (Screen.width / 2 - Image.width / 2, Screen.height / 2 - Image.height / 2, Image.width, Image.height), Image);
}
}
And this mini script, called LetterScript, has to be attached to your letter gameobject: (be sure to drag an imaage to the LetterImage variable!)
var LetterImage : Texture2D;
Assets/Raycast.js(19,50): BCE0005: $$anonymous$$ identifier: 'LetterScript'.
nar did you make the script called LetterScript with the short text above? and put it on the gameobject?
get_main can only be called from the main thread. Constructors and field initializers will be executed from the loading thread when loading a scene. Don't use this function in the constructor or field initializers, ins$$anonymous$$d move initialization code to the Awake or Start function.
get_main can only be called from the main thread. Constructors and field initializers will be executed from the loading thread when loading a scene. Don't use this function in the constructor or field initializers, ins$$anonymous$$d move initialization code to the Awake or Start function.Blockquote
Blockquote
I don't really know what I'm doing but the way I got this to work was by cutting the "var cam : Transform = Camera.main.transform;" line and pasting it above "var ray = Ray(cam.position, cam.forward)"
Answer by YoungDeveloper · Oct 13, 2013 at 07:59 PM
For this you will have to use:
*Raycasting, for getting what are you looking at.
*Create some item structure, so player would know what item is what, item logo etc.
*Getcomponent in runtime to access object you want to pick up
*Create tags for pickable objects
*Other stuff, questions which will come by learning and trying to script things
Your answer
![](https://koobas.hobune.stream/wayback/20220613115852im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Need a little help with a pick up script 1 Answer
I'm making a horror game and I need help with item pickup 1 Answer
Working With GUI 1 Answer