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 SuperMasterBlasterLaser · Jun 16, 2014 at 04:59 PM · c#2draycastmouse

Raycast2D always "remembers" it's last hit

Hello.

I have multiple 2D sprites in my scene and I got GameObject that just tracks mouse and checks if it hovers the other GameObjects with colliders:

     void Update () {
         Vector3 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition); 
         RaycastHit2D hit = Physics2D.Raycast(pos, Vector2.zero);
 
         if (hit.collider != null) {
             Debug.Log("YES");
         } else {
             Debug.Log("NO");
         }
 
     }

So when I first launch my scene it shows NO. But after it hits one of theese objects its says YES and not change even if my mouse does not goes over another objects anymore. What I have missed? I know that there is OnMouseEnter, OnMouseLeave functions, but I do not want to write it to every script.

Comment
Add comment · Show 6
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 meat5000 ♦ · Jun 16, 2014 at 05:12 PM 1
Share

ScreenToWorldPoint requires a Vector3. Z is the depth of the point.

avatar image AndyMartin458 · Jun 16, 2014 at 05:12 PM 0
Share

Are you sure that you want to Raycast with the Vector2.zero? That will basically not raycast in any direction, and I believe it will only detect if the screen point is within an object.

avatar image SuperMasterBlasterLaser · Jun 17, 2014 at 06:19 AM 0
Share

I have found it from forums. What do you suggest?

All I want to do is write YES when mouse over some object with 2D collider and NO when it is not. I can't use On$$anonymous$$ouseEnter like functions.

avatar image SuperMasterBlasterLaser · Jun 17, 2014 at 03:11 PM 0
Share

O$$anonymous$$ I if Vector3.Z is depth then I changed code like that:

    Vector3 mousePosition = Input.mousePosition;
             mousePosition.z = -10f;
             Vector3 pos = Camera.main.ScreenToWorldPoint(mousePosition); 
             RaycastHit2D hit = Physics2D.Raycast(pos, Vector2.zero);
 

Still the same.

avatar image Minchuilla · Jun 17, 2014 at 04:11 PM 0
Share

maybe you should consider trying viewportToWorldPoint ins$$anonymous$$d

Show more comments

2 Replies

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

Answer by SuperMasterBlasterLaser · Jun 17, 2014 at 04:30 PM

I have found solution. It Actually shows me YES and NO in situation that I want. Editor groups messages from Debug.Log that are same in one row and just shows its numbers. Then it sorts it alphabeically and when maximize on play is turned on it always shows me the LAST message.

This made me to think that Debug.Log always shows me YES. How I found it out? I just made counter and attached it like that:

 if (hit.collider != null) {
 Debug.Log("YES " + counter);
 } else {
 Debug.Log("NO " + counter);
 }

When I started the game now It showed me what I want. So, I am sorry for wasting your time, anyway thank you for attention.

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 AndyMartin458 · Jun 17, 2014 at 04:38 PM 0
Share

@Super$$anonymous$$asterBlasterLaser oh I see. Good thinking.

avatar image
0

Answer by SquigglyFrog · Jun 17, 2014 at 05:41 PM

Another way to do this is instead of using a debug.log, use a print statement.. it doesnt seem to group those.. found this issue with grouping myself.. print is just easier than using a counter variable..

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 Minchuilla · Jun 17, 2014 at 06:23 PM 0
Share

or you could just make sure collapse is not enabled in the console or look how many times it has been logged on the right of the notification

avatar image Pyrian · Jun 17, 2014 at 06:23 PM 0
Share

I've never noticed the grouping. I always put lots of information into my Debug.Log's. Between the specific call, the object calling, and the timestamp, there's never anything to group...

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

26 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Raycasting in 2D is not working 0 Answers

Clamp a mouse around several 2d objects 1 Answer

Raycast for a 2D objects 0 Answers

2d Raycast problem from enemy to player C# 1 Answer

Setting Velocity of Rigidbody to Mouse Speed 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