- Home /
AI Raycast problem
I'm drafting a system where an AI sends out a raycast to a target to check if there is something in front of it and i'm not sure how to go about it. I have a boolean variable called canSeeTarget which if it's set to true the AI draws a green line or if it equals false it draws a red line.
I have another variable which called target which is a transform. So if canSeeTarget equals true, the AI will move towards the target. This may sound a bit unclear so I'll write an example of what I'm doing in javascript
var target : Transform;
var canSeeTarget : boolean = false;
var targetDistance : int;
var thisAI : GameObject;
function Awake()
{
canSeeTarget = false;
}
function Update()
{
targetDistance = Vector3.Distance(target.position, transform.position);
if(canSeeTarget == true)
{
Debug.DrawLine(transform.position, target.position, Color.green);
thisAI.animation.CrossFade("WalkAim");
}
if(canSeeTarget == false)
{
Debug.DrawLine(transform.position, target.position, Color.red);
thisAI.animation.CrossFade("Idle");
}
}
This script only draws a line to the target and changes colour when canSeeTarget is changed. What I want to do is send a raycast to check if the target can actually be seen instead of seeing through objects and if it has been seen, I can tell it what to do from there. Any suggestions or help would be greatly appreciated
Thanks, Jordz
Answer by robertbu · Jun 04, 2013 at 02:55 PM
canSeeTarget = false;
var hit : RaycastHit;
if (Physics.Linecast(transform.position, target.position, hit) {
if (hit.collider.name == "TargtsName") {
canSeeTarget = true;
}
}
Note this only tests to see if the pivot position of the AI can see the pivot position of the target. You may want to raycast from an empty game object at the eyes of the AI. And you may want to test the corners of the bounding box of the target rather than just the pivot.
I'm getting errors which I don't understand, here how I wrote the code in the script:
var hit : RaycastHit;
if (Physics.LineCast(transform.position, target.position, hit))
{
if (hit.collider.tag == "Friendly")
{
canSeeTarget = true;
}
}
I have also tried Physics.Raycast and still have errors. What am I doing wrong?
It is Linecast (small 'c'). Sorry about that. Here is code that compiles:
#pragma strict
var target : Transform;
function Update() {
var hit : RaycastHit;
var canSeeTarget = false;
if (Physics.Linecast(transform.position, target.position, hit))
{
if (hit.collider.tag == "Friendly")
{
canSeeTarget = true;
}
}
}
Now that I think about it, you should do the other way around if there is a possibility of multiple "Friendly" game objects:
#pragma strict
var target : Transform;
function Update() {
var hit : RaycastHit;
var canSeeTarget = false;
if (Physics.Linecast(target.position, transform.position, hit))
{
if (hit.collider.tag == "Player")
{
canSeeTarget = true;
}
}
}
If there were multiple friendly game objects, the original code would give a false positive if another friendly was in the way of the target.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Finding RayCastHit's Origin Position 2 Answers
Raycasting and enemies ai 2 Answers
Diagonal Raycasting to detect platform returns true, even when false 1 Answer
Enemy AI using raycasting.. **Solved** 2 Answers