I need my enemy to target the player and move towards the player and attack without moving through walls. My script used to work(everything except moving through walls) but I changed it to try to make the enemy not move through walls and now the enemy won’t move at all. I attached a rigidbody, a character motor, a box collider, and a character controller. Heres my EnemyAI script.
using UnityEngine;
using System.Collections;
public class EnemyAI : MonoBehaviour {
public Transform target;
public int moveSpeed;
public int rotationSpeed;
private Transform myTransform;
public int maxDistance;
// Use this for initialization
void Awake () {
myTransform = transform;
}
void Start(){
GameObject go = GameObject.FindGameObjectWithTag("player");
target = go.transform;
maxDistance = 2;
}
// Update is called once per frame
void FixedUpdate () {
myTransform.rotation = Quaternion.Slerp (myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);
if(Vector3.Distance(target.position, myTransform.position) > maxDistance) {
rigidbody.velocity = transform.TransformDirection(new Vector3(0,0, moveSpeed));
}
}
}