- Home /
Enemy AI problem
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.
Forgive me if I'm mistaken, but it looks like you are assigning the value of your variable "rocketShooted" but you aren't actually checking its value before firing a rocket. You should probably add &&!rocketShooted to your if statement in the rocket script.
Answer by ben-tmg · Mar 10, 2014 at 10:07 PM
I think the problem is that you are starting a new coroutine every frame (because Update is called every frame). If you only want it to fire every ten seconds then keep track of when it last fired and only fire again when 10 seconds has passed.
ie
var lastFired = 0;
var secondsPerShot = 10;
void Update()
{
lastFired += Time.deltaTime;
if(lastFired > secondsPerShot)
{
//Fire...
lastFired = 0;
}
}
If you want it to fire immediately then set lastFired to 11 in Start.
Your answer
Follow this Question
Related Questions
[I REALLY NEED HELP FAST]Help with enemy Shooting 1 Answer
Enemy shooting player driving me crazy! 2 Answers
Clones of enemies will not shoot 0 Answers
Enemy fire does not propel in any direction? 1 Answer
2D Enemy AI range problem 2 Answers