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 Anymeese · Aug 05, 2014 at 09:25 PM · c#raycastlayerslayerlayermask

Detecting that I'm clicking a unit even though I'm not?

I'm working on a Turn Based Strategy game and have an issue.

Here is what the game looks like right now with 2 enemy units and one of my own units that I bought from the base (currently the purple sphere. Yay placeholders!) http://i.imgur.com/SiDyfdG.png


In my script, during Update, I call this segment of code:

 //if player left clicks on a unit
 if (Input.GetMouseButtonDown(0)
     && Physics.Raycast(ray, out hit, 500f, gameControl.getUnitLayer()))
 {
     Debug.DrawRay(ray.origin, ray.direction, Color.white, 100f);
     selectUnit(hit.collider.gameObject);
 }


Now here is the exact same scene shown above, but with the camera only showing layer 11, "Units" http://i.imgur.com/zUqcDAE.png


Yet the code segment is running every time I click the mouse, even if I'm very obviously not hitting anything on the units layer.. Here's a picture in the scene view of some of the draw rays, clearly not hiting any of the 3 units: http://i.imgur.com/DNJR3AV.png


Lastly, in other areas of the code, I have the information as follows. The values ARE NEVER CHANGED.

 Ray ray = mainCam.ScreenPointToRay(Input.mousePosition);
 private const int unitsLayer = 1 << 11;


And that's all the information. So why is it detecting the raycast every time I click the mouse, even if I'm not clicking on a unit???

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 ♦ · Aug 05, 2014 at 09:35 PM 0
Share

The drawray only draws if your raycast comes back true. There is a problem with the ray itself, or the layer.

Really it should not make a difference but try

 Ray ray = mainCam.ScreenPointToRay(new Vector3(Input.mousePosition,0));

The z position is ignored but the function still requires a Vector3. Ive added 'new Vector3(' as that is given in the exaple in the docs.

avatar image Anymeese · Aug 05, 2014 at 10:39 PM 0
Share

Nope, did not help anything :/ Had to do it this way, but still, no change.

 ray = mainCam.ScreenPointToRay(new Vector3(Input.mousePosition.x, Input.mousePosition.y,0));
avatar image meat5000 ♦ · Aug 05, 2014 at 10:45 PM 0
Share

Do what does

 Debug.Log(hit.collider.gameObject.ToString());

say?

avatar image Anymeese · Aug 05, 2014 at 10:57 PM 0
Share

gives me the name of the gameobject/unit that is being selected even though I'm not clicking on it. For some reason, although i have two different placeholder units, the issue is ONLY there for one of the two

avatar image Kiwasi · Aug 05, 2014 at 11:27 PM 0
Share

Is your raycast hitting a trigger? You can turn off raycasts hit triggers in the physics section.

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

23 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

Related Questions

LayerMask for RayCast 1 Answer

Layermask (raycast) wont work... 4 Answers

How to create User layer that acts like the "Ignore Raycast" layer? 1 Answer

ArgumentException: NameToLayer can only be called from the main thread 1 Answer

Raycast not working 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