My AI is being stubborn and I was wondering if their is a quick fix to my problem… I have a box colliders on everything from walls to player to AI, but for some reason my Enemy wants to faze through walls, and keeps pushing my character when it’s shouldn’t… i was wondering if their is a quick fix to this problem I provided the script and a picture of the problem… “NO Error in script”
*I want my enemy to stop at a distance of .25 away from player… I want my enemy to avoid and not go through walls…
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(SphereCollider))]
public class EnemyAI : MonoBehaviour {
public float preceptionRadius = 10;
public Transform target;
public float movementSpeed = 5;
public float rotationSpeed = 420;
private Transform myTransform;
// Use this for initialization
void Start ()
{
SphereCollider sc = GetComponent<SphereCollider>();
CharacterController cc = GetComponent<CharacterController>();
if (sc == null)
Debug.LogError("No Collider on enemy");
else
{
sc.isTrigger = true;
}
if(cc == null)
{
Debug.LogError("There's no Character Controller");
}
else
{
sc.center = cc.center;
sc.radius = preceptionRadius;
}
myTransform = transform;
}
// Update is called once per frame
void Update()
{
if (target)
{
Vector3 dir = (target.position - myTransform.position).normalized;
float direction = Vector3.Dot(dir, transform.forward);
float dist = Vector3.Distance(target.position, myTransform.position);
//Find or Look at Target
myTransform.rotation = Quaternion.Lerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);
//Movement
myTransform.position += myTransform.forward * movementSpeed * Time.deltaTime;
}
}
public void OnTriggerEnter(Collider other)
{
Debug.Log("Entered");
if (other.CompareTag("Player"))
{
target = other.transform;
}
}
public void OnTriggerExit(Collider other)
{
Debug.Log("Exit");
if(other.CompareTag("Player"))
{
target = null;
}
}
}
