• 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
2
Question by THECAKEISALIE93 · May 05, 2014 at 06:18 PM · raycastingtagmouseclick

Use raycast to click on object

I am trying to have a raycast on the mouse position. T$$anonymous$$s will allow you to click on an object, the raycast then detects what tag the object is under, then it will do a certain t$$anonymous$$ng. I have tested my script many times but to no avail. I just want someone to look over my function and to see if they see somet$$anonymous$$ng I am not. I am testing it out by having a quad, with a collider set up with the tag "HealthUpButton", and the raycast on mouse position. Script is below.

var levelUpPoints : int;

var curHealthLevel : int;

 function LevelUp() {
 
     var l$$anonymous$$t : RaycastHit;
     var lray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
 
     if(Input.GetMouseButtonDown) {
         
         if(Physics.Raycast(lray, l$$anonymous$$t, 100)){
         
             if (l$$anonymous$$t.transform.tag == "HealthUpButton") {
             
                 if(levelUpPoints > 0) {
                 
                     levelUpPoints --;
                     curHealthLevel += 1;
                 
                     }
                 }
 
             }
 
         }
 
     }
Comment
Add comment · Show 1
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 Jeff-Kesselman · May 05, 2014 at 06:36 PM 0
Share

s there a good reason you are limiting the length of the ray?

Id try it with infinity first and get ti working. Then reduce the range if you want

3 Replies

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

Answer by THECAKEISALIE93 · May 13, 2014 at 01:43 AM

All of you were right, I tried all of your methods and they worked. But in the end I went with a GUI window that can be dragged. Although I did not use all of the methods you supplied for my inventory, I will be implementing them on the interactive computers that will be in my game. Everyone here was a huuuuuge help!!!!

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
avatar image
1

Answer by wibble82 · May 05, 2014 at 06:47 PM

There is any number of reasons your code might not be working - maybe the ray cast isn't $$anonymous$$tting the object, or its $$anonymous$$tting somet$$anonymous$$ng but not "HealthUpButton", or perhaps that final if statement isn't being $$anonymous$$t.

First step, you need to add some 'Debug.Log' lines in your code. Change it to t$$anonymous$$s:

 function LevelUp() {
  
     var l$$anonymous$$t : RaycastHit;
     var lray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  
  
     if(Input.GetMouseButtonDown) {
        Debug.Log("Doing ray test");
        if(Physics.Raycast(lray, l$$anonymous$$t, 100)){
          Debug.Log("Hit somet$$anonymous$$ng: " + l$$anonymous$$t.transform.tag); 
          if (l$$anonymous$$t.transform.tag == "HealthUpButton") {
           Debug.Log("Hit the health up button");
           if(levelUpPoints > 0) {
               Debug.Log("Deducting points");
               levelUpPoints --;
               curHealthLevel += 1;
  
               }
           }
  
          }
  
        }
     }

The debug logs will tell you where its getting to, and allow you to diagnose w$$anonymous$$ch bit of the code isn't working. In my experience t$$anonymous$$s can be:

  • Your ray just isn't $$anonymous$$tting anyt$$anonymous$$ng! Try removing the limit to being of length 100 to start with

  • Your object doesn't have a collider (or the collider is weirdly positioned)

  • Your object is in the IgnoreRaycast layer

  • A common gotya - the 'l$$anonymous$$t.transform' refers to the transform of the object that contains the rigid body of the collider that was $$anonymous$$t! T$$anonymous$$s won't always be the same as the collider that was $$anonymous$$t. To get the exact collider use l$$anonymous$$t.collider. So you could check l$$anonymous$$t.collider.tag

Hope that helps

-Chris

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
avatar image
0

Answer by Jeff-Kesselman · May 05, 2014 at 06:38 PM

T$$anonymous$$s is a bit of a waste since you could use http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnMouseDown.html

But, make sure you have a collider on the object you are casting against and that it isn't in the IgnoreRaycast layer. I'd also start with an infinite length ray and only shorten it once that much is working.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

22 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 avatar image avatar image avatar image

Related Questions

Click Point From Raycasting Not Yielding Expected Coordinates 1 Answer

Get a raycast to hit only child objects to use axes 1 Answer

How can I differentiate between colliding with an object or its child? 1 Answer

How to destroy specific gameobject with a mouse click once player is looking at them using a Ray 1 Answer

I am stuck and don't know what to do, After upgrading to 2019 mouse and raycast don't work well 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