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 appferreira · May 21, 2012 at 01:47 PM · mouseclickmouseclickmouseoverover

Mouse Over & Mouse Click

Hi there ppl.

Im having some kind of problem with using RayCast hits and Input.GetButton simultaneosly. Can you please help me ?

What i want to do is a mouse over in a normal gameobject, and a mouse click too in the same gameobject. I tried many things and searched a lot before i post this here, and actually i have this simple comparison :

 function Update () {
     var hit : RaycastHit;
     if(Input.GetButtonDown("Fire1")) {
          if(Physics.Raycast(Vector3(Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z-200), transform.TransformDirection(Vector3.forward), hit, distance))
               Debug.Log("Mouse DOWN " + hit.point);
     }
 }

I correctly get the debug log message saying where my mouse CLICKED ... and if i do this :

 function Update () {
     var hit : RaycastHit;
     if (Physics.Raycast(Vector3(Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z-200), transform.TransformDirection(Vector3.forward), hit, distance)) {
           Debug.Log("Mouse OVER " + hit.point);
     }
 }

I correctly get the debug log message saying where my mouse IS MOVING. But instead, when i try to use both simultaneosly like this :

 function Update () {
     var hit : RaycastHit;
     if (Physics.Raycast(Vector3(Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z-200), transform.TransformDirection(Vector3.forward), hit, distance)) {
           Debug.Log("Mouse OVER " + hit.point);
     }
     if(Input.GetButtonDown("Fire1")) {
          if(Physics.Raycast(Vector3(Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z-200), transform.TransformDirection(Vector3.forward), hit, distance))
               Debug.Log("Mouse DOWN " + hit.point);
     }
 }

I only get the MOUSE OVER information and never get the information about the click! Can you please help me understanding what m i doing wrong and whats the best way to achieve what i want ?

Thank you in advance.

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 Berenger · May 21, 2012 at 02:32 PM

You're not using Raycast correctly.

First, be aware that Unity provides functions doing exactly that, OnMouseXXX() (like Update, it's called by Unity, you just need to implement it), with some small differences. The script implementing them must be attached to the objects you want to interact with, not to the object casting the ray currently. The ray is cast for all layers except IgnoreRaycast, which can be expensive on some devices and you cannot change it.

About your raycast now. Your rays' origines are incorrect and yet it's working, I suspect by luck. The origine must be in world space but Input.mousePosition is in screen space. Also, transform.forward is already local, so applying TransformDirection to it is actually matrix matrix (0,0,1), that's not what you want I suppose. If you don't want to use OnMouseXXX, try that :

 var hit : RaycastHit;
 if(Input.GetButtonDown("Fire1")) {
     var ray : Ray = Camera.main.ScreenPointToRay( Input.mousePosition );
     if(Physics.Raycast(ray, hit, distance))
           Debug.Log("Mouse DOWN " + hit.point);
 }


Comment
Add comment · Show 1 · 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 appferreira · May 21, 2012 at 11:28 PM 0
Share

Thank you very much for your answer!

I finded that my problem was happening because i was using "var hit" on both raycast (to mouse over) and mouse input - when i use var hit to mouseover and var hit2 to mouseclick - everything works fine!

$$anonymous$$orrow i will try to use the events that u talk about, and in the other places of my game that i really need to use raycast i will try to use your trick about the screen space - thats very important!

Thank you very much one more time!

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

Raycast hit not detected on cube 1 Answer

Detecting mouse click on GameObject with any mouse button. 2 Answers

MouseOver different Objects C# 1 Answer

Unity mouse input gradually becomes off 0 Answers

How to check if mouse over object without adding so many scripts? 2 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