- Home /
Problem is not reproducible or outdated
Problem With Enemy AI
Hey, i have modelled a figure from 3Ds Max and have imported it into Unity and set a number a codes as my game is FPS and the code i am having trouble with is the code which allows the AI to move towards the player to attack.
The problem being is that the enemy AI moves through the terrain and then continues to go downwards, here is the code i have assigned to the enemy:
using UnityEngine; using System.Collections;
public class EnemyAI : MonoBehaviour { public Transform target;
public int moveSpeed;
public int rotationSpeed;
public int maxDistance;
privateTransform myTransform;
void Awake(){
myTransform = transform;
}
// Use this for initialization void Start() {
GameObject go = GameObject.FindGameObjectWithTag("Player");
target = go.transform;
maxDistance = 2;
}
// Update is called once per frame void Update(){ Debug.DrawLine(target.position, myTransform.position);
//Look at target
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);
if(Vector3.Distance(target.position, myTransform.position) > maxDistance) {
//Move towards target
myTransform.position += myTransform.forward moveSpeed Time.deltaTime;
}
} }
All help would be much appreciated!
Follow this Question
Related Questions
Make unity NavMesh ignore terrain trees? 4 Answers
AI Script walking on terrain 1 Answer
Decipher 2 lines of code please 1 Answer
Automatic nav mesh generation 2 Answers
Is there way to see component as code? 0 Answers