• 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
Question by FuguFirecracker · Aug 01, 2014 at 12:48 AM · c#collisionraycast

Raycast Mouse Click On Specific Objects Only

I'm trying to place cube primitives on a surface I've tagged as "Floor" When one clicks on the floor, a cube is placed at the mouse coordinates.

My trouble is that I'm trying to limit the cube placement to occur only when the "Floor" is clicked.

I want the function to ignore the cubes... That is don't place a cube on top of another cube.

Any suggestions on how exclude the cube's collider?

Here is my most recent attempt, which doesn't work.

 public class GetMouseCoordinates : MonoBehaviour
     {
     
         public Material theBlockMaterial;
     
         void Update()
         {
             CastBlock();
         }
     
         private void CastBlock()
         {
             if (Input.GetMouseButtonDown(0))
             {
                 RaycastHit hit;
                 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                 if (collider.Raycast(ray, out hit, 1000.000f))
                 {
                     if (hit.collider.tag == "Floor")
                     {
                         GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
                         cube.transform.position = new Vector3(Mathf.Floor(hit.point.x), Mathf.Floor(hit.point.y) + 0.5f, Mathf.Floor(hit.point.z));
                         cube.renderer.material = theBlockMaterial;
                         cube.transform.parent = this.transform;
                   
                     }
                 }
             }
         }
     }





Comment

People who like this

0 Show 0
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

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by VesuvianPrime · Aug 01, 2014 at 01:16 AM

A couple suggestions:

1) have you considered adding a script to the floor object and implementing OnMouseDown?

2) you could filter your raycast by using LayerMasks

Comment

People who like this

0 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 FuguFirecracker · Aug 01, 2014 at 02:31 AM 0
Share

Hey Thanks for the suggestions.

I'm trying to stay away from OnMouseDown, as I plan on refactoring for touch and I don't believe that there is an equivalent OnTouch event. My understanding is that Raycast is required for touch events and OnMouseDown, while it will work, has performance issues on mobile.

Going to look into LayerMasks now.

Thanks

avatar image FuguFirecracker · Aug 01, 2014 at 01:26 PM 0
Share

After much experimentation... My Code works as expected when I manually -through Unity editor - place cubes or other primitives with colliders attached into the scene.

It is only the primitives created in script that don't behave, even though they are created with box colliders which should block my raycast from hitting the "Floor".

Oh... And LayerMasks did not work.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Collider Vision AI question. Solved! 0 Answers

Physics2D.Raycast(localPos vs pos, Vector2.up) ? 1 Answer

Why is the ray cast pointing towards the origin of the scene? 4 Answers

Smooth Movement on Geometry Collission? 0 Answers

Surface with hole and Raycast - Which collider 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