Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 DoctorMoney · Sep 08, 2013 at 02:03 AM · gameobjectguitextureinventorypickup

Pick Up objects when crosshair is over object

Basically I want to know if it's possible to detect if a guitexture is over an object (within a certain distance) and when a button is pressed that object is picked up.

I searched around and found detecting if a mouse is over an object but my cursor isn't always centered so it didn't work for me.

If anyone could point me in the right direction or explain how I'd go about doing this it'd be greatly appreciated!

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

1 Reply

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

Answer by aldonaletto · Sep 08, 2013 at 02:26 AM

You could pass the GUITexture position to ViewportPointToRay and use the resulting ray in Raycast. The code below was adapted from the ViewPortPointToRay example, and should be attached to the GUITexture object:

 function Update () {
     // Get the ray going through the GUI position
     var ray : Ray = Camera.main.ViewportPointToRay(transform.position);
     // Do a raycast
     var hit : RaycastHit;
     if (Physics.Raycast (ray, hit))
         print ("I'm looking at " + hit.transform.name);
     else
         print ("I'm looking at nothing!");
 }

The GUITexture property pixelInset must be centered: its X and Y components must be equal to -Width/2 and -Right/2 (like X=-64, Y=-29, Width=128, Height=58, for instance - this is the default settings when you create a new GUITexture).

NOTE: If the crosshair is always centered in the screen, you can just pass Vector3(0.5, 0.5, 0) to ViewportPointToRay:

     var ray : Ray = Camera.main.ViewportPointToRay(Vector3(0.5, 0.5, 0));




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 DoctorMoney · Sep 08, 2013 at 02:58 PM 0
Share

I'm at work so I can't test it with my actual game but I'm unsupervised I made a test area and tried it and it works. Thanks :D!

One question though, how would I get the name of what i'm looking at to another script on another object so I'd be able to test it and add the right values to the right variables?

@aldonaletto

avatar image aldonaletto · Sep 08, 2013 at 06:30 PM 0
Share

The script above is intended to be attached to the GUITexture, but it can be modified and attached to the object you want like below. It was also modified to do a raycast only when the left mouse button is pressed:

 public var guiTex: GUITexture; // drag here the GUITexture from the Hierarchy view
 
 function Update () {
   if (Input.Get$$anonymous$$ouseButtonDown(0)){ // if left button pressed...
     // Get the ray going through the guiTex position
     var ray : Ray = Camera.main.ViewportPointToRay(guiTex.transform.position);
     // Do a raycast
     var hit : RaycastHit;
     if (Physics.Raycast (ray, hit)){
       // hit.transform.name has the name of the object clicked
       // do whatever you want with it
     }
   }
 }
avatar image DoctorMoney · Sep 09, 2013 at 12:50 AM 0
Share

I've been testing the script on my actual game and it works but i can't seem to destroy the object

 private var objectToDestroy : GameObject;
 
 public var guiTex: GUITexture; // drag here the GUITexture from the Hierarchy view
  
 function Update () {
   if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E))    // if left button pressed...
     { 
     // Get the ray going through the guiTex position
     var ray : Ray = Camera.main.ViewportPointToRay(guiTex.transform.position);
     // Do a raycast
     var hit : RaycastHit;
     if (Physics.Raycast (ray, hit))
         {
           if (hit.transform.name == "rock")
               {
               objectToDestroy = GameObject.FindGameObjectWithTag("Pickup");
               print("$$anonymous$$$$anonymous$$$$anonymous$$ THAT IS A ROC$$anonymous$$");
               Destroy (objectToDestroy);
             }
           }
     }
 }

I figured I couldn't go Destroy(hit.transform) and I couldn't so I tried a work around but it wouldn't work either. How would I get it to destroy what i'm looking at?

Sorry for so many sub questions but this should be the last one :) Thank you for helping! @aldonaletto

avatar image aldonaletto · Sep 09, 2013 at 03:17 AM 0
Share

Don't use Find to find the object you've just clicked - the wrong one may be returned, and you already have all the info you need in the hit structure (any reference allows direct access to object properties like gameObject):

 public var guiTex: GUITexture; // drag here the GUITexture from the Hierarchy view
  
 function Update () {
   if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E))  // if left button pressed...
     { 
     // Get the ray going through the guiTex position
     var ray : Ray = Camera.main.ViewportPointToRay(guiTex.transform.position);
     // Do a raycast
     var hit : RaycastHit;
     if (Physics.Raycast (ray, hit))
       {
       if (hit.transform.name == "rock")
         {
            Destroy (hit.transform.gameObject); // this is the object you've clicked!
         }
       }
     }
 }
avatar image DoctorMoney · Sep 09, 2013 at 03:35 PM 0
Share

Thank you so much for your help @aldonaletto

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

16 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

Related Questions

Trying to make a simple inventory: 0 Answers

Slowly diminishing healthbar + objects that add to the healthbar. 2 Answers

Searching your inventory for an item,How do i search for an item in my inventory? 1 Answer

Collection Script troubles 1 Answer

Picking up Objects (from within the player's code) 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