Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 DocteurCox · Jun 19, 2013 at 02:27 PM · raycastnguilayercallbackonmouse

Raycast not working ?

Hello there !

I'm having a problem with Raycasting. I'm using NGUI and I'm trying to implement some universal OnMouse(Enter/Exit/...)-like callback, but no matter what I try, I can't get it working. Here's the full code :

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(Collider))]
 public class cpTip : MonoBehaviour {
 
     // Use this for initialization
     void Start () 
     {
         
     }
     
     // Update is called once per frame
     void Update () 
     {
         //Debug.Log("Update()");
         Camera cam;
         Color color;
         if ( gameObject.GetComponent<UIWidget>() != null)
         {
             cam = UICamera.currentCamera;
             color = Color.red;
             Debug.Log(gameObject.name + " : UI Camera");
         }
         else
         {
             cam = Camera.mainCamera;
             color = Color.blue;
             Debug.Log(gameObject.name + " : Main Camera");
         }
         Ray ray = cam.ScreenPointToRay(Input.mousePosition);
         RaycastHit hit;
         if ( Physics.Raycast(ray, out hit, 100, gameObject.layer ) )
         {
             Debug.Log("Mouse over object");
         }
         Debug.DrawLine( ray.origin, ray.origin+ray.direction*10, color, 0, true);
     }
     
     void OnMouseOver()
     {
         //Debug.Log("OnMouseOver()");
     }
 }

Can someone tell me what I'm doing wrong here ?

Thanks in advance :)

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 whydoidoit · Jun 19, 2013 at 02:33 PM 0
Share

So normally you can just add an OnHover method to the thing with the collider and that will then get the mouse over events (using NGUI).

avatar image DocteurCox · Jun 19, 2013 at 02:38 PM 0
Share

$$anonymous$$y objective is to create a tooltip system. That tooltip system must be usable with NGUI widgets, but also 3d objects in my scene. That's why I'm trying to make it generic. :)

avatar image whydoidoit · Jun 19, 2013 at 02:41 PM 0
Share

Ah got it :)

1 Reply

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

Answer by robertbu · Jun 19, 2013 at 02:35 PM

Layer and LayerMask are not the same thing. You need to use the layer to set the correct bit in the mask...something like:

  if ( Physics.Raycast(ray, out hit, 100, 1 << gameObject.layer ) )
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 DocteurCox · Jun 19, 2013 at 02:38 PM 0
Share

I'll have a look at it, thanks for your answer !

Edit : And it's working just fine. Thanks for the quick 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

15 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

Related Questions

How do I tell what NavMeshLayer a NavMeshAgent is currently on? 0 Answers

Layermask (raycast) wont work... 4 Answers

Unable to set collision layer from scrip 0 Answers

GetComponent, gameObject.tag vs gameObject.layer 1 Answer

How do i make my own ignore raycast user layer? 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