- Home /
Make AI attack on sight, otherwise go to another target.
I have a AI walking towards a moving target at all time which I made work by the code in the else-statement but when I add a raycast to make the AI see the player both the players and the AI's mesh is bugged and wont move nor make the animations that it did before. Here is the code I'm using now
var target : Transform;
var player : Transform;
var source : Transform;
var hit : RaycastHit;
function Update () {
if(collider.Raycast(new Ray(source.transform.position, Vector3.forward), hit, 10.0)){
if(hit.transform.tag == "player"){
GetComponent(NavMeshAgent).destination = player.position;
}
}
else{
GetComponent(NavMeshAgent).destination = target.position;
}
}
which is really small and simple but I don't seem to find what I'm missing and can't see why this affects the mesh rendering.
I tried once again with this code:
var target : Transform;
function Update () {
GetComponent(Nav$$anonymous$$eshAgent).destination = target.position;
}
and that did not work, once again only the light and sound moves around but not the body, same with the player.
Edit: I was able to fix the mesh by replace them but the code still doesn't work. Although it goes straight to the else-statement even if I stand right in front of it.
You are casting rays at Vector3.forward which is a global direction and is not affected by the object's rotation. You should try transform.forward ins$$anonymous$$d. Your "player" variable might also be set to null, try adding player=hit.gameObject under the tag check.
I tried this:
var goal : Transform;
var player : Transform;
var source : Transform;
var hit : RaycastHit;
function Update () {
if(Physics.Raycast(source.transform.position, transform.forward, hit, 30.0)){
if(hit.transform.tag == "player"){
GetComponent(Nav$$anonymous$$eshAgent).destination = player.position;
}
else{
GetComponent(Nav$$anonymous$$eshAgent).destination = goal.position;
}
}
else{
GetComponent(Nav$$anonymous$$eshAgent).destination = goal.position;
}
}
But it did not work, the AI just walks right through.
Edit: I don't really understand what you mean about adding player=hit.gameObject since I've tried in several different places with no further success.
Try replacing the line:
if(collider.Raycast(new Ray(source.transform.position, Vector3.forward), hit, 10.0)) {
With:
if (Physics.Raycast (source.transform.position, Vector3.forward, hit, 10.0)) {
As stated in the comment above I tried that and by looking at other scripts I can't see why this wouldn't work.
Answer by Khada · Mar 10, 2013 at 01:57 AM
Are you trying to pick towards the player or only directly in front of the AI entity? If you're trying to pick towards the player the ray should be:
Ray ray = new Ray(transform.position, (player.transform.position - transform.position).normalized);
If you want to pick directly in front of the AI entity, the ray should be:
Ray ray = new Ray(transform.position, transform.forwards);
Be mindful that 'if(Physics.Raycast)' will return the FIRST collider it hits, so if your AI entity has colliders and you don't use a layer mask when raycasting, you may just be hitting the AI entities' collider.
Also, are you sure 10 far enough for the ray cast and have you checked that the raycast is hitting the player? Use the following line inside a successful raycast to check:
Debug.Log(hit.collider.tag);
Thank you, it worked when I used mask.value and added layer and such. The debug log really helped too, thanks! Although he can look through walls which have a collider. Shouldn't the Physics.Raycast or the layer make so that doesn't happen?
It worked with the tips from above until a couple of hours ago when I applied it to another character.
var goal : Transform;
var player : Transform;
var source : Transform;
var hit : RaycastHit;
var mask : Layer$$anonymous$$ask = 0;
var chasingPlayer = false;
function Update(){
var ray = new Ray(source.transform.position, transform.TransformDirection(transform.forward));
if(chasingPlayer == true){
GetComponent(Nav$$anonymous$$eshAgent).destination = player.position;
GetComponent(Nav$$anonymous$$eshAgent).speed = 7.5;
Debug.Log("Chasing Player");
}
else if(collider.Raycast(ray, hit, 30.0)){
if(hit.collider.tag == "Player"){
GetComponent(Nav$$anonymous$$eshAgent).destination = player.position;
chasingPlayer = true;
Debug.Log(hit.collider.tag);
}
else{
GetComponent(Nav$$anonymous$$eshAgent).destination = goal.position;
Debug.Log("1");
}
}
else{
GetComponent(Nav$$anonymous$$eshAgent).destination = goal.position;
Debug.Log("2");
}
}
That is the code and I can't figure it out, it seems to be something wrong with the ray since it can move to the goal but doesn't make a notice of the player walking right through it. Also if I set chasingPlayer to true it will chase without any failure. This is the same ray I used before on another character but that one is also broken now. $$anonymous$$y tags are the same and also the layers.
As before, you're not using a layer mask and so you are probably detecting the npc's collider. This should be a comment or new question, not an answer.
I saw somewhere that it would work with -1 and it did for some ways but now it doesn't. But I try with 0, 1 and different types and still doesn't work.