- Home /
How to detect certain object using raycast
Hi guys,
I have a problem which i can't figure out. I want to do something like this:
If my camera's raycast*(field of view) hits a certain object(enemy) it will run an action(for now just a debug.log)*. But it doesn't ignore other objects. So if a wall stands in front of the enemy. The raycast doesn't notice the enemy.
I have searched on google and UnityAnswers, and came up with masklayers. But i didn't knew how that worked.
var layerMask = 1 << 8;
I tried the reference code that they used:
function Update () {
// Bit shift the index of the layer (8) to get a bit mask
var layerMask = 1 << 8;
// This would cast rays only against colliders in layer 8.
// But instead we want to collide against everything except layer 8. The ~ operator does this, it inverts a bitmask.
layerMask = ~layerMask;
var hit : RaycastHit;
// Does the ray intersect any objects excluding the player layer
if (Physics.Raycast (transform.position, transform.TransformDirection (Vector3.forward), hit, Mathf.Infinity, layerMask)) {
Debug.DrawRay (transform.position, transform.TransformDirection (Vector3.forward) * hit.distance, Color.yellow);
print ("Did Hit");
} else {
Debug.DrawRay (transform.position, transform.TransformDirection (Vector3.forward) *1000, Color.white);
print ("Did not Hit");
}
}
But again, i dont know how to set up a layer in the script.
Can anybody help me with this? Or knows a tutorial?
Thanks in advance! Darryl Donohoe
I tried this one:
var target: Transform; // the target transform private var hit: RaycastHit;
function Update() {
var fwd = transform.TransformDirection (Vector3.forward);
if (Physics.Raycast (transform.position, fwd, 10)) hit.transform == target;
Debug.Log("There is something in front of the object!");
}
But even this script displays the log when it hits an object. Doesn't matter which one.
So i think i got it!
Here is the code
function Update() {
var hit: RaycastHit; var forward = transform.TransformDirection (Vector3.forward);
if (Physics.Raycast (transform.position, forward, hit))
if(hit.collider.CompareTag("AISpy")){
Debug.Log("hit");
}else{
// Something blocking line of sight
// Enemy should engage if out of sight
}
// Show in the editor the ray
Debug.DrawLine (transform.position, hit.point);
}
But since this is a single raycast, it isn't that accurate. Is there a faster and a more efficient way to produce more raycasts without telling what each one has to do?
Like for my AI i have 3 raycasts and i had to script what happened if the left right or middle raycast hit an obstacle. But it would be easier, if i had something like a function. And if one of the raycasts hit the object, it would check the function ins$$anonymous$$d. So that i wouldn't have to copy every action
Answer by aldonaletto · Jul 08, 2012 at 04:48 PM
There's a much easier way to check visibility: use a Linecast from the character to the target, and check if the hit object is the target - if not, something is in between:
var target: Transform; // the target transform ... private var hit: RaycastHit; if (Physics.Linecast(transform.position, target.position, hit) && hit.transform == target){ // target is visible }If you want to also check if the target is inside the viewing angle, use this:
var viewAngle: float = 120; // define the viewing angle
function CanSeeTarget(target: Transform): boolean { var hit: RaycastHit; return Vector3.Angle(transform.forward, target.position-transform.position)
If i understand correctly, the linecast would check even though you are facing a different direction.
But the viewAngle isn't working for me, i also tried the one in the reference section. But that one can see through walls.
I have to say that i am new to scripting. I followed some basic tutorials. And after that, tried to make this work. I also tried the onBecameVisible function, but that one can also see through walls. I am working on this for about a week now and still can't figure it out.
Your answer
![](https://koobas.hobune.stream/wayback/20220613073856im_/https://answers.unity.com/themes/thub/images/avi.jpg)