Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by $$anonymous$$ · Oct 13, 2013 at 07:55 PM · guipickuppapernote

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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;
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image nar · Feb 23, 2014 at 02:41 AM 0
Share

Assets/Raycast.js(19,50): BCE0005: $$anonymous$$ identifier: 'LetterScript'.

avatar image Pigifying · Mar 07, 2014 at 03:33 AM 0
Share

nar did you make the script called LetterScript with the short text above? and put it on the gameobject?

avatar image maxjadin · Mar 13, 2014 at 01:19 AM 0
Share

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.

avatar image cargo1983 · Sep 23, 2014 at 12:28 AM 0
Share

Blockquote

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

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)"

avatar image Xtreme Gamers · Jan 19, 2015 at 10:09 PM 0
Share

will this work with picking up guns

avatar image
1

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Need a little help with a pick up script 1 Answer

Show GUI.Label when touching trigger 4 Answers

How to disable my Script and Gui 1 Answer

texture and text on gui 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges