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 rapowke · Jun 02, 2013 at 10:49 PM · raycastraycasthitgetmousebuttondown

Getting no response from Raycast

Hi, i'm trying to do a simple point click code, but strangely, nothing happens at all. Here is my code, what am i doing wrong?

 function Update ()
 {
     if(Input.GetMouseButtonDown(0))
     {
         print("Click!");
 
         var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
         var hit : RaycastHit;
                 
         if (Physics.Raycast (ray, hit, 100.0))
         {
             print("Hit!");
         }
     }
 
 }
Comment
Add comment · Show 3
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 Benproductions1 · Jun 03, 2013 at 12:16 AM 0
Share

Do the things you try to click have a collier attached? Have you tried to debug the ray?

avatar image rapowke · Jun 03, 2013 at 07:51 AM 0
Share

I'm fairly new to this so i don't know entirely what you mean, but the thing i want to click is a simple cube with no addons. I also want to mention that i don't get any printout: No "Click!", No "Hit!". How do i debug the ray?

avatar image Jamora · Jun 03, 2013 at 08:07 AM 0
Share

The code works. Are you trying to press on the cube in the scene-view, ins$$anonymous$$d of the game-view? Another thing I've seen people new to Unity do is forget to press Play before doing tests like these.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by phxvyper · Jun 03, 2013 at 08:12 AM

Firstly, the print function is used for Debug. So, unless you're trying to log to the Debug Console/Log File, you're using the wrong function.

If you wish to notify after a click/hit, then create a string and associated it in the parameters of a GUI TextField or TextArea...

 var action = "";
 
 OnGUI() {
 
     GUI.TextField(0, 0, new Rect(0, 0, 100, 20), action);
 
 }
 
 Update() {

     if(Input.GetMouseButtonDown(0)) {

         action = "Click!";
 
         var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         var hit : RaycastHit;
 
         if (Physics.Raycast(ray, hit, 100.0)) {
         
             action = "Hit!";
     
         }

     }
 
 }

This is display a TextField on the top left of the screen, whenever the LeftMouseButton is clicked, it will display "Click!". If, after the click, the Raycast hits the cube, then it will display "Hit!".

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 rapowke · Jun 03, 2013 at 10:28 AM

Neither of it is seems to be the problem. Do i need to add to a component?

Comment
Add comment · Show 3 · 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 phxvyper · Jun 09, 2013 at 09:09 PM 0
Share

@rapowke sorry for not responding. Anyways, when testing your game, you are pressing the play button, right? And then when you change something you're stopping the game then playing it again? Also, did you try out the code i suggested?

avatar image rapowke · Jun 10, 2013 at 09:54 PM 0
Share

Yeah I do. I think i found the answer. I added it to a component and it seems to work now.

avatar image phxvyper · Oct 05, 2013 at 10:15 PM 0
Share

@rapowke Hey! If my answer solved your issue, please accept it as the final answer by clicking on the checkmark to the left of my answer.

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

17 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

Related Questions

Is this way of making a raycast correct 2 Answers

RaycastHit2D.collider is null - why? 1 Answer

How do I change the raycasthit material back? 1 Answer

Raycasting will not hit some imported meshes with mesh colliders? 2 Answers

Physics.Raycast not hitting anything 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