Guys, I need help with enemy AI script that will follow Player, and will shoot Player, when locked on on him.
I've got this enemy script:
#pragma strict
var target : Transform; //the enemy's target
var moveSpeed = 14; //move speed
var rotationSpeed = 3; //speed of turning
var myTransform : Transform; //current transform data of this enemy
function Awake()
{
myTransform = transform; //cache transform data for easy access/preformance
}
function Start()
{
target = GameObject.FindWithTag("Player").transform; //target the player
}
function FixedUpdate () {
//rotate to look at the player
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
//move towards the player
transform.Translate(0, 0, moveSpeed * Time.deltaTime);
}
function OnCollisionEnter(collision : Collision)
{
if (collision.gameObject.tag == "Rocket")
{
Destroy(gameObject);
}
}
My enemy is plane(as well as player), and I want to add script that will rotate enemy(script will look on my players Z axis and transform my enemies to similar number, so if my player rotates to the right side, enemy will do the same). Also I have a script for shooting rockets:
using UnityEngine;
using System.Collections;
public class EnemyRocketSpawnSource : MonoBehaviour {
public GameObject EnemyRocketPrefab;
public static bool rocketShooted;
void Update () {
if(EnemyTargetDetector.TargetLocked)
{
Instantiate(EnemyRocketPrefab, transform.position, transform.rotation);
StartCoroutine("RocketShooted");
}
}
IEnumerator RocketShooted()
{
print ("RocketShooted Function Called");
rocketShooted = true;
yield return new WaitForSeconds(5);
rocketShooted = false;
}
}
My TargetDetector works perfectly, but when it comes to shooting rockets, enemy plane shoots tons of them into the air. And I want to shoot one each 10 seconds. How can I achieve this? Please help me.
↧