- Home /
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 behind a tree, or behind an object. I believe the easiest way to do this, 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 behind 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 this? As in, line cast to a specific object to detect colliders?
Thanks.
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 behind 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 child of something, as that might get you some odd results.
I've tried doing something 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 this helped :)
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.
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 :)
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
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/
Thankyou! This really helped, and now I now how to use layermasks, (Albeit of a basic level)
Your answer
Follow this Question
Related Questions
Simple In game search field 2 Answers
Ray cast collider recognition and execution of a script attached to hit object 1 Answer
Creating a single-line function for GameObject.Find and GetComponent (for multiple components) 3 Answers
how do I get reference to a parent or a child? 2 Answers
If ray in contact with collider... 1 Answer