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 Oakie · May 02, 2012 at 12:13 AM · raycastgetmousebuttondown

Raycast and OnMouseButtonDown combination problem

My game is your typical point-and-click adventure puzzle game. You have an inventory of items that you can select and use on your surroundings. I'm having trouble getting my code that will allow you to use an item on something else in the scene.

Using the debugs it appears that the raycast is working correctly, so it appears that the GetMouseButtonDown(0) is where I'm running into problems. I also tried GetButtonDown("Mouse X") but that didn't work either.

I've gotten one iteration of this code to work when I applied it to the object, however it stopped working whenever I made a build or closed unity and opened it up again. It was very strange... Thanks in advance for any help!

 var camera2 : Camera;
 
 var inventoryDropDown : inventory_drop_down;
 
 function Update(){
 //sees if the candle is selected from the inventory and to make sure the main camera is enabled and not one of the zoomed in ones
   if (inventoryDropDown.item_candle == true && camera2.enabled == true){
   
     interact();
   }
   
 }
 
 function interact(){
 
   var mousePos : Vector3 = Input.mousePosition;
   var ray : Ray = camera2.ScreenPointToRay (Input.mousePosition);
   var hit : RaycastHit;
 
   if (collider.Raycast (ray, hit, 100.0)){
     Debug.Log ("candle is hoving over curtains");   
     if (Input.GetMouseButtonDown(0)){
       Debug.Log("Interacted with Object. fun times.");
       Destroy(gameObject);
     
     }
   }
 }

alt text

Screen shot 2012-05-02 at 1.47.19 PM.png (191.3 kB)
Comment
Add comment · Show 4
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 syclamoth · May 02, 2012 at 12:16 AM 0
Share

Do you want it to be a single-click thing, or a constant hold thing? What exactly is the failure mode?

avatar image Oakie · May 02, 2012 at 12:31 AM 0
Share

Single click is what we're going for. I'm sorry, what do you mean by failure mode? I'm new to coding, so I'm pretty ignorant of a lot of the lingo.

avatar image syclamoth · May 02, 2012 at 12:52 AM 1
Share

Sorry, that wasn't programmer lingo- it was engineering lingo. I sometimes forget that not everyone is constantly surrounded by engineers...

By 'failure mode' I mean the exact circumstances and method my which it fails. The assumption is that if something fails, it will fail in a way that is identifiable and quantifiable. A 'failure mode' is a way of identifying specific types of failures (so that you can apply certain troubleshooting patterns to them depending on the mode).

From the looks of things, the object should delete itself if you click the left mouse button while hovering over the 'curtain' object. I assume from your question that the 'hovering' is working correctly- which makes this a tricky one, because I'm not aware of any way in which Input.Get$$anonymous$$ouseButtonDown can fail on you like that! It's possible that there's something else going on here. Do you have any GUI code that might be interfering with the mouseClicks? Any GUI component that receives mouse clicks can prevent Get$$anonymous$$ouseButtonDown from working (because it eats the UI events). Try disabling all the other objects in the scene, and see if it works then.

avatar image Oakie · May 02, 2012 at 05:50 PM 0
Share

Ah okay, yeah the failure mode appears to occur when it tries to access the Get$$anonymous$$ouseButtonDown. I took your advice and disabled all the scripts containing OnGUI and the above code now works. Thank you! I did not realize that GUI could screw with the Get$$anonymous$$ouseButtonDown function.

Is there a way to get around this problem? The way I have the inventory set up is that you can mouse over the top part of the screen and it comes down. You can then click on an object and it becomes the cursor to indicate that it's the object you're about to use. Would I have to design another way to use items? I uploaded a screenshot to my original post so you can see what I mean.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Seth-Bergman · May 02, 2012 at 12:56 AM

you need to get the "if (Input.GetMouseButtonDown(0)){" into the update function.. Try this:

 var camera2 : Camera;
 
 var inventoryDropDown : inventory_drop_down;
 
 var canInteract : boolean;
 
 function Update(){
 //sees if the candle is selected from the inventory and to make sure the main camera is enabled and not one of the zoomed in ones
   if (inventoryDropDown.item_candle == true && camera2.enabled == true){
 
     interact();
   }
 else
 canInteract = false;
 if (Input.GetMouseButtonDown(0) && canInteract){
       Debug.Log("Interacted with Object. fun times.");
       Destroy(gameObject);
 
     }
 
 
 }
 
 function interact(){
 
   var mousePos : Vector3 = Input.mousePosition;
   var ray : Ray = camera2.ScreenPointToRay (Input.mousePosition);
   var hit : RaycastHit;
 
   if (collider.Raycast (ray, hit, 100.0)){
     Debug.Log ("candle is hoving over curtains");   
     canInteract = true;
   }
 else
 canInteract = false;
 }
Comment
Add comment · Show 6 · 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 syclamoth · May 02, 2012 at 01:10 AM 0
Share

No, I'm pretty sure that's not it. The Input function is already inside Update- Update calls to 'interact', and the Input function occurs inside that. There's nothing that says that you can only put Input functions directly inside Update- they work perfectly well outside of there so long as the stack eventually leads back there. In any case, there are bugs in this code. If the camera gets disabled, or the inventoryDropDown changes while the mouse is over the object, canInteract will remain true.

avatar image Seth-Bergman · May 02, 2012 at 01:19 AM 0
Share

ok, logic wasn't perfect, bug was easy enough to fix though (fixed it now).. I still think this may be the issue though..

Or not, you may be right

avatar image syclamoth · May 02, 2012 at 01:25 AM 0
Share

I think there's something else happening here. What you've done there is a simple enough refactoring, but it doesn't change anything about what actually gets called when (it just rearranges the parts of the code). Here's a slightly cleaner alternative:

 function Update(){
     //sees if the candle is selected from the inventory and to make sure the main camera is enabled and not one of the zoomed in ones
     if (inventoryDropDown.item_candle == true && camera2.enabled == true){
         if (interact() && Input.Get$$anonymous$$ouseButtonDown(0)){
             Debug.Log("Interacted with Object. fun times.");
             Destroy(gameObject);
         }
     }
 }
 
 function interact(): boolean{
     var mousePos : Vector3 = Input.mousePosition;
     var ray : Ray = camera2.ScreenPointToRay (Input.mousePosition);
     var hit : RaycastHit;
 
     if (collider.Raycast (ray, hit, 100.0)){
         Debug.Log ("candle is hoving over curtains");   
         return true;
     }
     return false;
 }
avatar image Seth-Bergman · May 02, 2012 at 01:33 AM 0
Share

touche... Anyway, you're probably right about the GUI thing, you seem pretty sure of yourself :)... Still, I would start with this, to eli$$anonymous$$ate the possibility..

avatar image syclamoth · May 02, 2012 at 01:36 AM 0
Share

Well, I'd never be one to turn down good troubleshooting, so it's worth a shot. Even still, I'm convinced that it's not that. If I had a Unity installation on the computer I'm using right now, I'd check it myself, but until then it's up to the original poster to decide what works and what doesn't.

Show more comments

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

what is the difference between raycast and getmousebuttondown in a RTS game ? 1 Answer

OnMouseDown and OnMouseDrag with RayCast 1 Answer

Raycast repeats multiple times 2 Answers

Raycasting vs. Input.GetMouseButtonDown 2 Answers

Raycast hit working in unity editor but not in deployed webplayer build? 0 Answers


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