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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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

Hey Thanks for the suggestions.

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

Going to look into Layer$$anonymous$$asks now.

Thanks

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

After much experimentation... $$anonymous$$y 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 Layer$$anonymous$$asks 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.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Align to grid in runtime problem 1 Answer

Raycast doesnt detect object in front of rigidbody (player is stuck on wall) 1 Answer

What's a reliable way to detect if an object is no longer being hit by a ray? 1 Answer

Create interchanging Raycast cone for flashlight 2 Answers

Can't Make Raycast Target Nearest Character 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