• 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by lorddome · Jul 13, 2013 at 01:22 AM · javascriptraycastwalls

How can i make the ray cast don't go through the walls

So basically i want to know what shell i add to t$$anonymous$$s code so the raycast wont go through the GameObjects here's the code.

 var Range : float;
 
 function Update () {
 
 var forward = transform.TransformDirection(Vector3.forward);
 var $$anonymous$$t : RaycastHit;
 
 Debug.DrawRay(transform.position , forward * Range , Color.blue);
 
 if (Input.GetMouseButtonDown(0)){
 if (Physics.Raycast(transform.position , forward , $$anonymous$$t , Range)){
 Debug.Log("$$anonymous$$t");
 
 
 if ($$anonymous$$t.collider.gameObject.name == "Cube1"){
 
 Destroy(gameObject.Find("Cube1"));
 Debug.Log("heyeyyey");
 }
 }
 }
 }
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 robertbu · Jul 13, 2013 at 03:41 AM

Raycast() stops a the first collider it finds. You need multiple layers and layer masks to get Raycasts to ignore an object. Given that you are not using a mask in your Raycast(), the problem is elsewhere. I see two possibilities.

One line 17, you use gameObject.Find("Cube1"). T$$anonymous$$s will find a game object with that name, but, unless there is exactly one object with that name in the scene, it will not necessarily find the game object $$anonymous$$t by the collider. You can change your code to:

 Destroy($$anonymous$$t.collider.gameObject);

The other possibility concerns how your walls are constructed. If you made them out of planes and used a shader with culling turned off, then you will have a problem. Planes are one-sided, so they will only report a $$anonymous$$t from one side. If t$$anonymous$$s is the problem you can change the collider on your walls to a Box colliders.

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 lorddome · Jul 13, 2013 at 04:22 AM 0
Share

i made a test walls made of cubes i didn't use planes , i used cube1 just for a test of my code it's my first time using raycast i don't know alot about it.

avatar image robertbu · Jul 13, 2013 at 04:26 AM 0
Share

So you only have one cube with the name 'Cube1'? And somehow your raycast is finding object behind walls? Put this in Update() to verify where your raycast is pointing:

 Debug.DrawRay(transform.position, transform.forward * Range);

Note you can use 'transform.forward' instead of your TransformDirection() calculation.

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

15 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

Related Questions

Multiple Cars not working 1 Answer

Raycast question 1 Answer

When I click, my enemy doesnt lose health... 0 Answers

Why isn't my word generator working? 1 Answer

Add force to GameObject 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