- Home /
This question was
closed Feb 18, 2014 at 05:22 PM by
robertbu for the following reason:
Duplicate Question/Don't code to spec
Question by
electfall70 · Feb 18, 2014 at 04:42 PM ·
enemyenemyaiwalls
Help AI going through walls
i need some help i made this AI but the enemy goes through walls this is my code (please write down the code so i can just copy it):
using UnityEngine;
using System.Collections;
public class EneMyAI : MonoBehaviour {
public Transform target;
public int moveSpeed;
public int rotationSpeed;
public int maxdistance;
private Transform myTransform;
void Awake(){
myTransform = transform;
}
void Start () {
GameObject go = GameObject.FindGameObjectWithTag("Player");
target = go.transform;
maxdistance = 2;
}
void Update () {
Debug.DrawLine(target.position, myTransform.position, Color.red);
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);
if(Vector3.Distance(target.position, myTransform.position) > maxdistance){
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
}
}
Comment
(please write down the code so i can just copy it):
Unity Answers is not for writing scripts for you. It is to help you write your own scripts.
In order to not go through walls, your character needs either a Rigidbody or a CharacterController. You need to explore these two topics.
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Solid enemy help 1 Answer
Enemy HELP 1 Answer
My Eneny AI Script Wont Work? 2 Answers