- Home /
AI passes through walls and flies
I've made an AI that follows the player when within range, attacks when very close, and goes idle when out of range. The AI flies around to get to the player and can pass through the walls of my building (imported from maya with "generate colliders" on).
I've tried adding a rigid body, but the AI falls through the building.
I'm trying to make the AI follow the building mesh as my player would, and would attack upon reaching the player but would not allow the AI to attack continuously as the player can only take 2 hits.
If someone can help me out, I'd greatly appreciate it :D I'm new to coding in c# and unity
here's my code: using UnityEngine; using System.Collections;
public class EnemyAI : MonoBehaviour {
public float Speed = 0.5f;
public GameObject ghost;
private float XMove;
private float YMove;
private RaycastHit rCast;
private Transform player;
private float distanceFromPlayer;
public static EnemyAI Instance;
private int hp = 100;
private int attack = 0;
void Awake(){
Instance = this;
Audio.Instance.audio.enabled = false;
}
void Start(){
player = GameObject.FindGameObjectWithTag("Player").transform;
}
void Update(){
distanceFromPlayer = Vector3.Distance(transform.position, player.position);
if(distanceFromPlayer <= 0.5f){
if(attack == 0){
attack = 1;
ghost.animation.CrossFade("G_Attack");
Audio.Instance.audio.enabled = false;
//yield WaitForSeconds = 2;
//while(ghost.animation.isPlaying){
if(Physics.Raycast(transform.position, transform.forward, out rCast)){
Debug.Log("Damage Taken");
hp = hp - 50;
Debug.Log("Player Health = " + hp);
if(hp <= 0){
Debug.Log("Player DEAD");
EnemyAI.Instance.enabled = false;
}
}
}
else{
}
//}
}
else if(distanceFromPlayer < 5){
//Debug.Log("Chase!");
transform.rotation = Quaternion.Slerp(transform.rotation,
Quaternion.LookRotation(player.position - transform.position), Time.deltaTime * 4);
transform.Translate(0,0,.05f);
ghost.animation.CrossFade("G_Walk");
Audio.Instance.audio.enabled = false;
attack = 0;
}
else{
//Debug.Log("Out of Range");
ghost.animation.CrossFade("G_Idle");
ghost.animation["G_Idle"].speed = .5f;
Audio.Instance.audio.enabled = true;
attack = 0;
}
}
Answer by markedagain · Jul 11, 2013 at 04:14 PM
i hated this problem when i had it.
your problem is that the character controller you are using is setting a global way of checking collisions and only its mesh is used for oncolission checks.
i was making a simple shooting game and the gun would not stop going trough walls, my solution was to stop using the character controller if memory serves me correct
I don't think he is using any character controller and that may be the reason why.
you are correct, actually looking at the code does show using translate and this does not take into account colliders.
Answer by fafase · Jul 11, 2013 at 04:17 PM
You should look into that: http://unitygems.com/basic-ai-character/
I think your issue is that you are moving the guy with the transform. Even though transform can detect collision, it just does not care about collider and simply gets your object where you tell it to go.
The character controller is a component that has more information for collision and prevent your guy from going through other colliders (as long as you do not travel at war speed).
Also if u are going to use the physics engine u should do it in the FixedUpdate ins$$anonymous$$d of Update function as its alot of overhead.
and if u still want to stick with doing the movement yourself u will have to do some raycasts to make sure there is nothing in front of you before moving
Your answer
Follow this Question
Related Questions
Enemy not rotating when inside range 1 Answer
How do I make an enemy lead his shots? 2 Answers
2D AI, Aim at player - even when jumping? 1 Answer
Make an enemy only rotate with navmesh agent 1 Answer
2D 360 degress platformer example needed 0 Answers