here is the enemy chase that im using in a game. for some reason when the enemy player moves towards my hero he sinks into the ground i think its because he "gravitates" towards the players feet (the xyz position of said feet) and it causes him to sink.... any suggestions ?
using UnityEngine;
using System.Collections;
public class EnemyAI : MonoBehaviour {
public Transform target; // transform = the x,y,z, coordinates
public int movespeed =1;
public int rotationspeed =1 ;
private Transform mytransform;
public int attackdistance = 15;
public Vector3 aggroradius;
void Awake ()
{
mytransform = transform; // caches the value of transform to optimize runtime (sorta)
}
// Use this for initialization
void Start () {
//finds the player
GameObject go = GameObject.FindGameObjectWithTag("Player");
// enemy target is the players x,y,z position...
target = go.transform;
}
// Update is called once per frame
void Update () {
//look at player
mytransform.rotation = Quaternion.Slerp(mytransform.rotation, Quaternion.LookRotation(target.position- mytransform.position), rotationspeed *Time.deltaTime);
//finds the distance between player and enemy
aggroradius = target.position - mytransform.position;
// if the positive value of the distance between the player and the enemy is less than the attack distance - hulk smash //jk
if (aggroradius.magnitude < attackdistance)
{
// move towards player
mytransform.position += mytransform.forward * movespeed * Time.deltaTime;
}
// do the funky chicken
animation.Play("walk");
// if (distance = good)
// attack ?
//animation.Play ("attack");
}
}
thanks !