- Home /
ray cast returning the wrong object, going through multiple objects
Dear community.....
I have a rough FOV object attached to an enemy gameobject.
When the fov object collide with the character, it sends a ray from the enemy to the character and check if there is an obstacle between them.
Unfortunately it seems like it's returning the last object instead of the first one, or objects at a specific distance from the caster.
Did I miss some aspect of RayCast ? or something else with my script ? Maybe is it not returning objects that are too close ? I am considering using RayCastAll but it seems too heavy for this simple task....
Thanks in advance for any help...
I am attaching a screen capture showing a ray going through a box then through the character and finally into a building the raycast is returning the building. I need it to return the box object.
Here is the code attached to the FOV object :
#pragma strict
private var script : enemyScript;
function OnTriggerStay(collided : Collider) {
if(collided.gameObject.name == "Character"){
var hit : RaycastHit;
if ( Physics.Raycast(transform.position, collided.transform.position, hit, 10)) {
Debug.Log (hit.transform.gameObject.name);// it should return the obstacle
Debug.DrawLine (transform.parent.position, hit.point, Color.red);
if(hit.transform.gameObject.name == "Character") {
transform.parent.GetComponent(enemyScript).state = AIState.Chasing;
}
}
}
}
function OnTriggerExit(collided2 : Collider) {
if(collided2.gameObject.name == "Character"){
transform.parent.GetComponent(enemyScript).state = AIState.Investigating;
}
}
yes it does, it is not a trigger though, I just tested setting it up as a trigger but nothing's changed
I understand that layers allow to isolate group of colliders, I am not sure if its what I need.... But I just found that I can allow raycast to interact with triggers, and it seems to work better that the standard box collider. So maybe I will set my colliders as triggers and isolate them on a layer so the rays won't interact with non wanted triggers. will keep you updated
hmm there 's something wrong with my setup now, I didn't try the trigger option, but my ray is not anymore pointing toward my player as it should....
Answer by robertbu · May 05, 2013 at 09:34 PM
This form of Raycast takes a position and a direction, so it should be:
if ( Physics.Raycast(transform.position, collided.transform.position - transform.position, hit, 10)) {
But you may want to use Linecast() instead:
if ( Physics.Linecast(transform.position, collided.transform.position, hit, 10)) {
that is very helpful, Linecast behaviour is better but still problematic. it seems to shoot only if my target is colliding with other objects....I also deactivated shooters's collider and rigidbody in inspector just in case...
oops sorry, my mistake it's seems to be working almost fine. I was expecting the Linecast to be shot even when there are no obstacle. The last problem is if my character is close to another object, like against a wall, but not behind it, the linecast return true. Do I need to shoot it just a little before the target ? or do I need to change the target collider ?
sorry for spam$$anonymous$$g, but I believe I can fix all that by adjusting the colliders. Thank ya all !