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 /
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 this 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 hit : RaycastHit;
 
 Debug.DrawRay(transform.position , forward * Range , Color.blue);
 
 if (Input.GetMouseButtonDown(0)){
 if (Physics.Raycast(transform.position , forward , hit , Range)){
 Debug.Log("hit");
 
 
 if (hit.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"). This 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 hit by the collider. You can change your code to:

 Destroy(hit.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 hit from one side. If this 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' ins$$anonymous$$d 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.

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

Help with Enemy AI 1 Answer

Accessing colliders, in if statement 2 Answers

How do I make a respawn system? 1 Answer

Sphere cast thing help please!!!! 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