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 question was closed Jun 29, 2015 at 05:07 PM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Sisym · Jun 29, 2015 at 09:47 AM · physics.raycast

Physics.Raycast not detecting objects between camera and player

I'm trying to make a script that disables the mesh renderers of objects that are between the player and the camera, so that if the camera moves through a wall, the player is still visible, since the object will be invisible (but still active). I've created a Physics.Raycast, but it isn't detecting walls that go between the player and the camera. Here's the script:

 void FixedUpdate ()
     {
         //reenable all object renderers that have been disabled
         if (disabledObjects.Count > 0) 
         {
             Debug.Log ("list had at least one object in it");
             foreach (GameObject gObject in disabledObjects) {
                 Renderer rend;
                 rend = gObject.GetComponent <Renderer> ();
                 rend.enabled = true;
             }
         }
 
         //clear the list, setting up to be refilled by new objects between camera and player.
         Debug.Log ("clearing list");
         disabledObjects.Clear ();
 
         //while loop to ensure each object detected by the raycast is disabled
         RaycastHit hitInfo;
         while (Physics.Raycast (transform.position, target.transform.position, out hitInfo)) 
         {
             Debug.Log ("there was something between the camera and the target");
             Renderer rend;
             rend = hitInfo.collider.GetComponent <Renderer> ();
             rend.enabled = false;
             disabledObjects.Add (hitInfo.collider);
         }
     }

What did I do wrong?

I'm pretty much completely new to Unity, please don't eat me if this is a stupid question.

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

2 Replies

  • Sort: 
avatar image
0
Best Answer

Answer by Sisym · Jun 29, 2015 at 04:55 PM

Ignore my previous comment (I can't delete it, since it hasn't been approved yet). I solved the problem. The problem was that I thought a ray was drawn between two points, so I simply gave it the position of the camera and the player, but I solved it when I realized the second argument was direction, rather than another point. I also figured out that I had to turn gizmos on in order to use Debug.RaycastDraw.

I definitely would not have been able to figure out Physics.RaycastAll though, thanks for the help!

Comment
Add comment · 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
1

Answer by Hellium · Jun 29, 2015 at 09:48 AM

The while loop won't do what you want.

Use Physics.RaycastAll instead of Physics.Raycast to get all the objects touched by the ray. In fact, disabling the mesh renderer won't let the ray pass through your object.

http://docs.unity3d.com/ScriptReference/Physics.RaycastAll.html

Comment
Add comment · Show 3 · 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 Sisym · Jun 29, 2015 at 04:37 PM 0
Share

Okay, so I used the Physics.RaycastAll method ins$$anonymous$$d of the while loop, and still got the same problem where it wasn't detecting objects between the player and the camera. I solved the issue by reversing target.transform.position and transform.position, but now the raycast is detecting objects that are in front of the player by about ~1.3 units. Debug.RaycastDraw isn't drawing anything for me, so I can't debug that way either.

So, why did reversing the transforms of the player and the camera solve the problem, and why is my raycast detecting objects clearly not between the player and the camera?

Thanks.

avatar image Hellium · Jun 29, 2015 at 05:05 PM 0
Share

I haven't notice but yes, you are right, a ray is defined by an origin and a direction.

Thanks for the feedback, don't forget to accept the answer by clicking on the check mark under the vote buttons ! ;)

avatar image meat5000 ♦ · Jun 29, 2015 at 05:08 PM 0
Share

You didnt get the answer dude.

But you certainly deserve some thumbs up, Hellium.( @Sisym this is your job )

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Shoot on spheres 0 Answers

event triggered by the wrong camera 0 Answers

Physics.Raycast lagging behind 1 Answer

Crosshair Distance Indicator 1 Answer

execution order of Destroy and Physisc.Raycast 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