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
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. This will allow you to click on an object, the raycast then detects what tag the object is under, then it will do a certain thing. 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 something 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 lhit : RaycastHit;
     var lray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
 
     if(Input.GetMouseButtonDown) {
         
         if(Physics.Raycast(lray, lhit, 100)){
         
             if (lhit.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 hitting the object, or its hitting something but not "HealthUpButton", or perhaps that final if statement isn't being hit.

First step, you need to add some 'Debug.Log' lines in your code. Change it to this:

 function LevelUp() {
  
     var lhit : RaycastHit;
     var lray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  
  
     if(Input.GetMouseButtonDown) {
        Debug.Log("Doing ray test");
        if(Physics.Raycast(lray, lhit, 100)){
          Debug.Log("Hit something: " + lhit.transform.tag); 
          if (lhit.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 which bit of the code isn't working. In my experience this can be:

  • Your ray just isn't hitting anything! 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 'lhit.transform' refers to the transform of the object that contains the rigid body of the collider that was hit! This won't always be the same as the collider that was hit. To get the exact collider use lhit.collider. So you could check lhit.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

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

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

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

question about instantiating 2 Answers

How to create gameobjects by clicking in the scene view in the editor? 1 Answer

Get a raycast to hit only child objects to use axes 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