- Home /
 
 
               Question by 
               TheLazyLima · Aug 25, 2017 at 02:20 PM · 
                c#aipathfinding  
              
 
              Collision detection script fails sometimes.
For the most part this script works, however every now and then an enemy will fail at pathfinding and go through a building or wall. Is there a way i can stop this?
using UnityEngine; using System.Collections;
namespace Daniel { public class EnemyAI : Living { // Detection private int range = 10; private float speed = 10f; private bool isThereAnyThing = false;
     // Waypoints/Targets
     public GameObject[] targets;
     private float rotationSpeed = 900f;
     private RaycastHit hit;
     GameObject target;
     [SerializeField]
     private int randomTarget = 0;
     [SerializeField]
     float timeToNextCheck = 3;
     public float effectTimer = 2f;
     public GameObject deathEffect;
     public LayerMask detectThis;
     
     void Start()
     {
         randomTarget = Random.Range(0, 8);
         target = targets[randomTarget];
     }
     void FixedUpdate()
     {
         timeToNextCheck = timeToNextCheck - Time.deltaTime;
         ScanForNewWaypoint();
         LookAtTarget();
         Move();
         CheckForObsticales();
     }
     void LookAtTarget()
     {
         //Look At Somthly Towards the Target if there is nothing in front.
         if (!isThereAnyThing)
         {
             Vector3 relativePos = target.transform.position - transform.position;
             Quaternion rotation = Quaternion.LookRotation(relativePos);
             transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime);
         }
     }
     void Move()
     {
         // Enemy translate in forward direction.
         transform.Translate(Vector3.forward * Time.deltaTime * speed);
     }
     public void CheckForObsticales()
     {
         //Checking for any Obstacle in front.
         // Two rays left and right to the object to detect the obstacle.
         Transform leftRay = transform;
         Transform rightRay = transform;
         //Use Phyics.RayCast to detect the obstacle
         if (Physics.Raycast(leftRay.position + (transform.right * 7f), transform.forward, out hit, range, detectThis) || Physics.Raycast(rightRay.position - (transform.right * 7f), transform.forward, out hit, range))
         {
             if (hit.collider.gameObject.CompareTag("Obstacles"))
             {
                 isThereAnyThing = true;
                 transform.Rotate(Vector3.up * Time.deltaTime * rotationSpeed);
             }
         }
         // Now Two More RayCast At The End of Object to detect that object has already pass the obsatacle.
         // Just making this boolean variable false it means there is nothing in front of object.
         if (Physics.Raycast(transform.position - (transform.forward * 4), transform.right, out hit, 10, detectThis) ||
          Physics.Raycast(transform.position - (transform.forward * 4), -transform.right, out hit, 10, detectThis))
         {
             if (hit.collider.gameObject.CompareTag("Obstacles"))
             {
                 isThereAnyThing = false;
             }
         }
     }
     public void ScanForNewWaypoint()
     {
         CheckForObsticales();
         if (timeToNextCheck <= 0)
         {
             timeToNextCheck = Random.Range(6, 3);
             randomTarget = Random.Range(0, 8);                
             target = targets[randomTarget];
         }
     }
     public override void TakeHit(float dmg, Vector3 hitPoint, Vector3 hitDirection)
     {            
         if (dmg >= health)
         {
             Destroy(Instantiate(deathEffect, hitPoint, Quaternion.FromToRotation(Vector3.forward, hitDirection)) as GameObject, effectTimer);
             Debug.Log("Exploded");
         }
         base.TakeHit(dmg, hitPoint, hitDirection);
     }
 }
 
               }
               Comment
              
 
               
              Your answer
 
             Follow this Question
Related Questions
How can I prevent a character from ignoring it's waypoint path? 1 Answer
Multiple Cars not working 1 Answer
Boids/Flocking Tutorial 0 Answers