Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 mabit · Jun 21, 2011 at 10:31 PM · mouseclickignore objects

Make Mouse Click Ignore Object

I have a problem where I have collectible objects which randomly spawn and enemies that wander around....occasionally an enemy will wander over a collectible meaning that if the player click on it they are clicking on the enemy and not on the collectible.

I have my objects on type of Object, Helpful Chars, Enemies, and Collectibles. They are all on different layers and I have tried layermasks.

Layer 8 Enemies Layer 9 Friendly Chars Layer 10 SunOrbs Layer 11 Ground

I have had it so that the raycast only pics up collectibles when mouseover, but this did not make any difference.

I have 2 scripts...One is to keep control of the mouse position and the raycasts

This is the GetMousePosition script

 static var MousePositionIn3DSpace: Ray;
 static var hit : RaycastHit;
 var layer : int = 10;
 var layer2 : int = 11;
 var maskingLayers : int;
 var maskingLayers2 : int;
 var sunOrbMask : int;
 
 
 function Update(){
     
     // Set all layers except layer 10 and 11 is visible to raycast
 maskingLayers = 1 << layer;
 maskingLayers2 = 1 << layer2;
     sunOrbMask = maskingLayers| maskingLayers2;
     
     Debug.Log(Get_Mouse_Position.hit.collider); // This shows that it is finding SunOrb
     
     MousePositionIn3DSpace = Camera.main.ScreenPointToRay (Input.mousePosition);
     
     if (Physics.Raycast (MousePositionIn3DSpace, hit, 100,sunOrbMask)) {
         Debug.DrawLine (MousePositionIn3DSpace.origin, hit.point);
         
     }
     
 }

Second Script is the Collectible Object script

 function OnMouseDown(){
 
     if (Get_Mouse_Position.hit.collider == "SunOrb"){
         Level_1.sunOrbPower = Level_1.sunOrbPower+5;
         Destroy(gameObject);
     }
     
 }


If anyone can help with this it would be appreciated. I am new to Unity and Coding coming from a pure design background so please explain in simple terms.

Thanks

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Waz · Jun 21, 2011 at 11:11 PM

A Raycast with the layer mask set should work, if the colliders of the items are on the layers you suggest, so you need to post code so we can see what you're doing wrong. Another option is to use RaycastAll, but note that the return is not ordered in any way, you need to sort or search it for your relevant object.

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 mabit · Jun 22, 2011 at 12:06 AM 0
Share

I have updated the post with some more information and code.

avatar image
0

Answer by Waz · Jun 22, 2011 at 12:35 AM

The problem is that your call to Raycast will trash your previous hit value even if nothing is hit. A quick fix is probably to only assign to hit if the hit occurs:

 var tmp : RaycastHit;
 if (Physics.Raycast(... tmp ...)) { hit = tmp; }
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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Click Point From Raycasting Not Yielding Expected Coordinates 1 Answer

on mouse click get other object to move away 2 Answers

Can I buy Grid Framework? 0 Answers

Create gameobject on mouse click? 1 Answer

How to highlight a box in a grid on mouse hover, action whenever clicked 0 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