- Home /
I need an explanation about raycast and collisions
Hey Guys!
So, I'm trying to script a simple AI routine for the enemy bots for my game.
Thanks to you all, I've managed to make a patrol using waypoints, now, I wanto to make this.
If the player enter in the enemy view, the enemy will pursuit and attack (melee), and if the enemy can't see the player anymore, backs to the patrol.
I believe that, the best way to do this, is using the raycast and colliders, so, can you guys tell me a little more about colliders and raycasts? I mean, the question is:
How can my enemies "see the walls"?
How can I use the colliders to make my enemies check if is a wall or the hero?
How can I use the raycast to make a viewpoint for my enemies?
And, how can I make my hero collide to the enemy's view, and NOT see behind the walls?
I hope don't act confuse (my English doesn't help, though), but I appreciate your kindness, anyway
Answer by aldonaletto · Sep 11, 2012 at 03:14 AM
The most used way to "see" a target without that annoying X-ray effect is to use Physics.Linecast(enemy.position, target.position), and check if the object hit is the target - if it's not, there's something in between:
function CanSeeTarget(target: Transform): boolean { var hit: RaycastHit; if (Physics.Linecast(transform.position, target.position, hit) && hit.transform == target){ return true; } return false; }You can also add more restrictions, like limited view range and angle:
var range: float = 100; // viewing range var viewAngle: float = 15; // viewing angle
function CanSeeTarget(target: Transform): boolean { var hit: RaycastHit; if (Physics.Linecast(transform.position, target.position, hit, range) && hit.transform == target && Vector3.Angle(target.position-transform.position, transform.forward) < viewAngle){ return true; } else { return false; } }
I've tryed in too many ways, but aways return the same error "Assets/IAEnemy.js(42,17): BCE0017: The best overload for the method 'IAEnemy.CanSeeTarget(UnityEngine.Transform)' is not compatible with the argument list '()'." That's my script so far ( It's a real mess, I know)
var Target : Transform; var PointA : Transform; var PointB : Transform; var range: float = 100; // viewing range var viewAngle: float = 15; // viewing angle
private var NavComponent : Nav$$anonymous$$eshAgent;
function Start () {
NavComponent = this.transform.GetComponent(Nav$$anonymous$$eshAgent);
BeARobot();
}
function CanSeeTarget(Target: Transform): boolean { var hit: RaycastHit; if (Physics.Linecast(transform.position, Target.position, hit, range) && hit.transform == Target && Vector3.Angle(Target.position-transform.position, transform.forward) < viewAngle){ return true; } else { return false; } }
function Update() {
if (CanSeeTarget()) print("I sense the enemy is near!");
// print("I sense the enemy is far!");
}
function walktoA () { NavComponent.SetDestination(PointA.position); animation.Play("walk"); } function wait () { animation.Play("idle"); } function walktoB() { NavComponent.SetDestination(PointB.position); animation.Play("walk"); } function seek() { NavComponent.SetDestination(Target.position); animation.Play("run"); }
function BeARobot() {
Invoke( "walktoA", 0.1 ); Invoke("wait", 3.0); Invoke( "walktoB", 10.0 ); Invoke("wait", 13.0); Invoke( "BeARobot", 15.0 ); }
You must pass the target transform to the function CanSeeTarget:
var player: Transform; // drag the player here ... if (CanSeeTarget(player)){ print("I can see you, bastard!"); }
Well, I'm Still having problems. The script is not showing errors anymore, however, The bot, doesn't see the player, And I don't have idea. $$anonymous$$y script so far is like this:
var player : Transform;
var PointA : Transform;
var PointB : Transform;
var range: float = 100; // viewing range
var viewAngle: float = 15; // viewing angle
private var NavComponent : Nav$$anonymous$$eshAgent;
function Start () {
NavComponent = this.transform.GetComponent(Nav$$anonymous$$eshAgent);
BeARobot();
}
function CanSeeTarget(player): boolean {
var hit: RaycastHit;
if (Physics.Linecast(transform.position, player.position, hit, range) && hit.transform == player && Vector3.Angle(player.position-transform.position, transform.forward) < viewAngle){
return true;
} else {
return false;
} }
function Update() {
if (CanSeeTarget(player))
{ print("I can see you, bastard!");
} else { print("I'm not working");} // It's just returning that
}
function walktoA () { NavComponent.SetDestination(PointA.position); animation.Play("walk"); } function wait () { animation.Play("idle"); } function walktoB() { NavComponent.SetDestination(PointB.position); animation.Play("walk"); } function seek() { NavComponent.SetDestination(player.position); animation.Play("run"); }
function BeARobot() {
Invoke( "walktoA", 0.1 ); Invoke("wait", 3.0); Invoke( "walktoB", 10.0 ); Invoke("wait", 13.0); Invoke( "BeARobot", 15.0 ); }
Your answer
Follow this Question
Related Questions
AI Shooting through walls. 1 Answer
Visible GameObjects for AI 0 Answers
Help me with AI 1 Answer
AI Raycast problem 1 Answer