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 lucabeisel · Nov 12, 2014 at 02:58 PM · raycastmousealpharaycastallonmouseexit

OnMouseExit Problems vs. RaycastAll

Hey guys,

thanks for being such a great community.

I have this grid of GameObjects and i want to make not only the one transparent that the mouse rests on, but also those behind it - so i am sending a RaycastAll to lower the alpha of those hit.

 RaycastHit[] hitall = Physics.RaycastAll (ray, 1000, ObjectLayer);

 for (int i = 0; i < hitall.Length; i++) 
 {
     RaycastHit hit = hitall [i];

     if (hit.collider.transform.childCount > 0) 
     {
         Summary: set alpha from children of hit.collider.GameObject to 0.3f
     }
 }

To reset the alpha back to normal when the mouse is not over the GOs anymore, i am using OnMouseExit on the Cube class.

 public void OnMouseExit()
     {
         if (this.transform.gameObject.layer != 8 && this.transform.childCount > 0)
         {
                 Summary: Set all childrens' transparency back to 0.7f
         }
     }


This seems to almost work, but weirdly enough when i move the mouse "inward" from the outside of the grid to the inside, the alpha of the GOs is not changed back except for the last one the cursor hit. When i move the cursor "outward", it works just like i intended.

alt text

Can anybody point out to me what the problem is here?

Thanks a lot in advance!

All the best.

problem.jpg (269.6 kB)
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 AlwaysSunny · Nov 12, 2014 at 03:07 PM 0
Share

I'm a bit unclear - RaycastAll method always works, but the On$$anonymous$$ouseExit sometimes fails? Can you deduce anything further about the scenario which might account for these see$$anonymous$$gly-random failures?

avatar image lucabeisel · Nov 13, 2014 at 10:09 PM 0
Share

It totally worked. If you post it as a proper answer, i can give you the green tick mark. Thanks a lot for your help!

avatar image AlwaysSunny · Nov 13, 2014 at 11:22 PM 0
Share

Cool, I'm glad you got it working. :3

2 Replies

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

Answer by JinxM_ · Nov 19, 2014 at 04:18 PM

Alternative guess here.. You're using RaycastAll to make things transparent, which goes through colliders.

Conversely, you're using OnMouseExit to turn things opaque, which might not go through colliders.

Due to the angle of the camera, when you move the mouse to the lower left, you reach a point where tiles overlap. OnMouseExit stops at the new blocking tile and turns the tile underneath opaque, but RaycastAll goes through and turns it transparent again.

When you move the mouse towards the upper right, OnMouseExit turns the tile opaque and RaycastAll also misses at the same time, resulting in the correct behaviour.

I'm not certain that's how OnMouseExit works, but it seems the likeliest explanation. AlwaysSunny has a good solution. An alternative is to save a list of the transparent tiles and turn opaque the ones that no longer appear in your next RaycastAll. (I would use the LINQ operator "Except" for this.)

Comment
Add comment · Show 2 · 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 AlwaysSunny · Nov 19, 2014 at 06:14 PM 0
Share

/facepalm/ - I have no clue why I didn't make that connection. +1 to you for making me feel silly while directly answering the OP's question. :)

avatar image lucabeisel · Nov 20, 2014 at 11:31 PM 0
Share

Thank you very much, that totally made sense.

For anybody reading this in the future - just go with AlwaysSunny's suggestion below. It's easy and works like a charm. Thanks a lot to both of you, i wish i could checkmark both answers.

avatar image
2

Answer by AlwaysSunny · Nov 12, 2014 at 11:35 PM

When I approach situations like this, where some temporary activity should change the state of an object, I generally try to avoid using any OnExit type of event. This is largely because certain conditions will bypass the OnExit family of calls, such as quitting or pausing and resuming the game under altered input conditions. Or, you run into weirdness like you're currently experiencing. They just don't seem ironclad, and I like ironclad, even at the expense of extra work and CPU workload.

If the situation permits - and it sounds like yours does - I'd let my RaycastAll method trigger a method on your objects which places them in a FadeOut state. Somewhere in LateUpdate, if FadeOut wasn't triggered this frame, switch to FadeIn. It feels a little sophomoric to do this, since you're creating extra work for the CPU, but if your events don't work under the most basic conditions and you can't determine why, this presents a workable alternative.

Make sense? Best of luck,

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

stop gun from rotating 1 Answer

Turn in the direction of movement 0 Answers

Mouse plane does not detect height 1 Answer

EteeskiTutorials' bullets (raycast bullets with gravity) 1 Answer

How do I detect when mouse passes over an object? 4 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