Step 1: Add a “Character Controller” to your enemy object
Step 2: Add the “EnemyFollow.js” to the enemy and set it to whatever
Step 3: Create a empty GameObject at the very tip of your enemy’s gun, and assign “EnemyGun.js” to it
Step 4: Assign everything to everything, and voila!
EnemyFollow.js – enemyHeight is the enemy’s Y axis so that it dosen’t fly and stays at the axis
#pragma strict
var player : Transform;
var enemyHeight : float = 1.43;
var speed : int = 3;
function Start () {
}
function Update () {
transform.LookAt(player);
transform.position.y = enemyHeight;
transform.position += transform.forward * speed * Time.deltaTime;
}
function OnTriggerEnter(col : Collider) {
if(col.tag == "Bullet") {
Destroy(gameObject);
}
}
EnemyGun.js
var projectile : Rigidbody;
var speed = 10;
var player : Transform;
var shotS : AudioClip;
function Start() {
var rendum = Random.Range(1F,3F);
InvokeRepeating("Shuut", 2, rendum);
}
function Update() {
transform.LookAt(player);
}
function Shuut () {
audio.PlayOneShot(shotS);
clone = Instantiate(projectile, transform.position, transform.rotation);
clone.velocity = transform.TransformDirection( Vector3 (0, 0, speed));
Destroy (clone.gameObject, 0.5);
}
If you would want to give me credit in your game, thanks ^-^
NOTE! You can always put the enemy walk speed to static so you can change it after will through script by doing EnemyFollow.speed = 5 or something to change it ![]()