I made AI enemy that follows the player when the needed distance is met. I used Transform. But the enemy goes through walls. How can I fix the problem without changing the whole code? Thank you for your answer in advance! Here is my code:
using UnityEngine;
using System.Collections;
public class EnemySkeleton : MonoBehaviour {
public Transform player;
static Animator anim;
// Use this for initialization
void Start () {
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
if (Vector3.Distance(player.position, this.transform.position) < 10)
{
Vector3 direction = player.position - this.transform.position;
direction.y = 0;
this.transform.rotation = Quaternion.Slerp(this.transform.rotation,
Quaternion.LookRotation(direction), 0.1f);
anim.SetBool("IsIdle", false);
if (direction.magnitude > 2)
{
this.transform.Translate(0, 0, 0.08f);
anim.SetBool("IsWalking", true);
anim.SetBool("IsAttacking", false);
}
else
{
anim.SetBool("IsWalking", false);
anim.SetBool("IsAttacking", true);
}
}
else
{
anim.SetBool("IsWalking", false);
anim.SetBool("IsAttacking", false);
anim.SetBool("IsIdle", true);
}
}
}