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 mabit · Nov 26, 2012 at 03:29 AM · raycastobjecttouchblock

Make object block a raycast

I am using raycast to check for touches on object, however the problem I have is that the raycast is passing thru walls and activating the touch code on objects on the other side of the wall. For example on one side of the wall there is a torch and on the other is a chest. WWhen player touches the torch the chest on the other side opens.

Is there a way to stop raycasts from passing thru certain objects?

On Torch objects the code is

 function Update ()
 {
     if (Input.GetMouseButtonDown(0)) // check for left-mouse
     {
         if (torchLit == false){
             var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
             var hit : RaycastHit;
             if (collider && collider.Raycast (ray, hit, 100.0))
             {
                 if(hit.collider.tag == "Torch"){
                     OnMouseDown();
                 }
             }
         }
     }
 }

And on my chest object

 function Update () {
     if (Input.GetMouseButtonDown(0)) // check for left-mouse
     {
         var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
         var hit : RaycastHit;
         if (collider && collider.Raycast (ray, hit, 100.0))
         {
             if(hit.collider.tag == "chest"){
                 OnMouseDown();
             }
         }
     }
     if (chestOpen == true){
         ChestOpened();
     }
 }

Thanks in advance.

Comment
Add comment · Show 2
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 Avengerxp · Nov 09, 2013 at 04:40 PM 0
Share

I have the same problem.

I use this script

 var hit : RaycastHit;
 var matArray : $$anonymous$$aterial[];
 private var index : int;
 
 function Update () {     
  
   if (Input.Get$$anonymous$$ouseButtonDown(0) && collider.Raycast( Camera.main.ScreenPointToRay(Input.mousePosition), hit, $$anonymous$$athf.Infinity)) {
     index++;
     index = index % matArray.Length;
     renderer.material = matArray[index];
   }                       
 }

please update this function so that objects overlapping each other does not trigger at once..

avatar image aldonaletto · Nov 09, 2013 at 04:55 PM 0
Share

@Avengerxp, you should post this as a new question! Posting as an answer makes no sense and produces little or no result at all. Post a new question and explain more clearly what you expect to do and what it's doing wrong.

1 Reply

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

Answer by Democre · Nov 26, 2012 at 04:33 AM

You are using collider.Raycast, which in the docs says specifically that it "Casts a Ray that ignores all Colliders except this one."

What you would need to do is in some other Update function in a master script somewhere (so that it is only running once), use Physics.Raycast. In all the other objects just leave the OnMouseDown function that you have already, and remove the raycasts from their Update functions. This avoids the Raycasting from multiple objects each ignoring all other objects, and gives you one ray cast that will stop at the first object hit.

 //in some game controller script
 function Update () {
     if (Input.GetMouseButtonDown(0))
     {
         var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
         var hit : RaycastHit;
         if (Physics.Raycast (ray, hit, 100.0))
         {
             hit.collider.gameObject.SendMessage ("OnMouseDown", null, SendMessageOptions.DontRequireReceiver );
         }
     }
 }

Cheers!

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 mabit · Nov 26, 2012 at 04:39 AM 0
Share

Thanks for the advice, ill look into that tomorrow and see how I get on :)

avatar image mabit · Nov 28, 2012 at 12:06 AM 0
Share

Can thank you enough for a great answer and simple to implement.

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

13 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

Related Questions

Android touch 3d Object event 1 Answer

When I touch one object all others react 0 Answers

Ignore Touch 2 Answers

Managing a Raycast so that it only interacts with one object when two objects are in a similar position 2 Answers

i need help in walking character in android, using raycast where user touches? 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