• 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
Question by suctioncup · Aug 22, 2012 at 09:24 AM · gameobjectcolliderfindraylinecast

Locate a game object using rays

Ok,

I need to check if a gameObject is in the cameras view or not. I have got that working, (On a basic level), with Renderer.IsVisible. Ok. But now I need a away to check if he is be$$anonymous$$nd a tree, or be$$anonymous$$nd an object. I believe the easiest way to do t$$anonymous$$s, would be to use a ray.

I tried using line cast, but it didn't work. It shot off in a different direction.

         Debug.DrawRay(you.position, slenderMan.position, Color.green);
     if (!Physics.Linecast (you.position, slenderMan.position))
     {
     Debug.Log("He is be$$anonymous$$nd a collider");
     }

The debug line was never called, because when I looked in the screen view, the ray (Shown in green) was pointing in a different direction.

Is there a way to fix t$$anonymous$$s? As in, line cast to a specific object to detect colliders?

Thanks.

Comment
Kacer

People who like this

1 Show 0
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
Best Answer

Answer by Kacer · Aug 22, 2012 at 09:42 AM

Is there a reason why you're not using a raycast instead?

From my interpretation of the documentation you're checking whether there is a collider between you and the target, it returns true if there is a collider. So you should be getting a message saying that he is be$$anonymous$$nd a collider when he isnt,

As for the odd error you're getting, make sure the target or the origin of the ray isnt a c$$anonymous$$ld of somet$$anonymous$$ng, as that might get you some odd results.

I've tried doing somet$$anonymous$$ng similar myself, here's the code i made:

 public GameObject target;
 
     void Update () {
         Debug.DrawLine(transform.position,target.transform.position,Color.cyan);
         if(!Physics.Linecast(transform.position,target.transform.position)){
             Debug.Log("Hai World");
         }
     }

as it is written here the console prints "hai world" when there isnt a collider between the origin and the target.

Hope t$$anonymous$$s helped :)

Comment
suctioncup

People who like this

1 Show 6 · 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 suctioncup · Aug 22, 2012 at 10:16 AM 0
Share

Thankyou for this,

I figured out (partly) what my problem was. I needed to remove the !, but I also had to remove the collider on the second game object, otherwise the line cast was detecting that collider.

avatar image Kacer · Aug 22, 2012 at 10:53 AM 1
Share

You're welcome, remember to mark the question answered if you found out what the problem was, it helps other people who might have the same issues :)

avatar image suctioncup · Aug 22, 2012 at 10:55 AM 0
Share

Its not fixed yet. The linecast detects the collider on the second object, which causes imprecise data. I need both gameObjects to have a collider, but detect when a third collider is added it the middle. http://forum.unity3d.com/threads/148342-Unity-Linecast-not-detecting-colliders

avatar image Kacer · Aug 22, 2012 at 10:58 AM 1
Share

You could use a layermask; http://docs.unity3d.com/Documentation/ScriptReference/Physics.Linecast.html

using layermasks lets you decide what colliders should be detected by the layermask, ie; place the characters on layer 1 and the object on layer 2, then have the linecast ignore objects on layer 1.

Simple \o/

avatar image suctioncup · Aug 23, 2012 at 11:32 AM 0
Share

Thankyou! This really helped, and now I now how to use layermasks, (Albeit of a basic level)

Show more comments

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Ray cast collider recognition and execution of a script attached to hit object 1 Answer

Simple In game search field 2 Answers

Finding the angle between two GameObjects 1 Answer

Problems with the triggers(not recognized) 1 Answer

All the objects in my script choose the same target. 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