Hi there!! ^^
I made this simple EnemyAI for a game concept that i am currently developing … For the moment the enemies aren’t anything else than simple cubes with a Rigidbody attached to them … They respond to Physical properties and collides with other objects on the Scene … The thing is that whenever the Attack function starts the enemy starts to moving a little … let’s say “Laggy” o.O The weirdest thing is that this occurs only at certain diagonal angles while chasing the player.
It is difficult to explain it … I think it has something to do with the Quaternions … but as i barely made it to get those horrible things into my script i have no idea if they are “Compatible” or the best option to get along with the rest of the code … Any ideas? =)
using UnityEngine;
using System.Collections;
public class EnemyAI : MonoBehaviour {
public Transform player;
float distancefrom_player;
public float look_range = 20.0f;
public float agro_range= 10.0f;
public float move_speed= 5.0f;
void Update ()
{
distancefrom_player = Vector3.Distance (player.position, transform.position);
}
void LateUpdate(){
if (distancefrom_player < look_range )
{
transform.LookAt(player);
}
if (distancefrom_player < agro_range)
{
attack();
}
}
void lookAt()
{
Quaternion rotation = Quaternion.LookRotation (player.position - transform.position);
transform.rotation = Quaternion.Slerp (transform.rotation, rotation, Time.deltaTime);
}
void attack()
{
transform.Translate (Vector3.forward * move_speed * Time.deltaTime);
}
}