Question by
sjvlahadamis · Jun 27, 2017 at 11:59 PM ·
terrain3daigravity
Help to make enemy AI not go through terrain and not fly when chasing player up hills.
I am making a game for a school project and my ai keeps flying when following my player up a hill(terrain) and also goes through hills(terrain) if my player is on the other side or somewhat close to it. Help would be appreciated. My Ai have rigidbody on them with gravity enabled. Here is my aiCode:
using UnityEngine; using System.Collections;
public class aiScript : MonoBehaviour {
public float fpsTargetDistance;
public float enemyLookDistance;
public float attackDistance;
public float enemyMovementSpeed;
public float damping;
int MaxDist = 20;
int MinDist = 15;
public Transform fpsTarget;
Rigidbody enemyBody;
// Use this for initialization
void Start () {
enemyBody = GetComponent<Rigidbody>();
}
// Update is called once per frame
void FixedUpdate () {
fpsTargetDistance = Vector3.Distance(fpsTarget.position,transform.position);
if(fpsTargetDistance<enemyLookDistance){
lookAtPlayer();
}
if (fpsTargetDistance < attackDistance) {
attackPlease();
}
if (Vector3.Distance (transform.position, fpsTarget.position) >= MinDist) {
transform.position += transform.forward * enemyMovementSpeed * Time.deltaTime;
}
if (Vector3.Distance (transform.position, fpsTarget.position) <= MaxDist) {
//Here Call any function U want Like Shoot at here or something
}
}
void lookAtPlayer (){
Quaternion rotation = Quaternion.LookRotation(fpsTarget.position - transform.position);
transform.rotation = Quaternion.Slerp (transform.rotation, rotation, Time.deltaTime * damping);
}
void attackPlease (){
enemyBody.AddForce (transform.forward*enemyMovementSpeed);
}
}
Comment
Your answer
Follow this Question
Related Questions
How to keep vehicles in correct lanes 0 Answers
AI Character can't switch between different terrains 1 Answer
How do I cell shade terrain? 0 Answers
3D Aimable Sling Shot 0 Answers
How can I turn the arrow keys into touch controls? 0 Answers