- Home /
Simple Side Scrolling AI, firing a projectile in players general direction
I am working on a simplistic side scrolling 2d shooter, and I am trying to figure out how to record the players position at a certain point in time on the x/y plane, and then pass that variable onto my enemy script so that it will fire a bullet in the players general direction where he previously was. Right now I have it set so the enemy fires a bullet every few seconds but it goes in a straight line across the x plane. How would I move an instatiated object toward the players coordinates in a way it wasn't updated every frame?
-Here is the bullet code-
#pragma strict
var bulletSpeed : float = 5.0; //controls how fast the enemy bullet moves to the left on the x axis
function Start ()
{
}
function Update ()
{
//move the enemy bullet to the left on the x axis as soon as its spawned
transform.Translate(-bulletSpeed*Time.deltaTime,0,0);
}
-And here is my enemy controller code-
#pragma strict
var health : float = 50; //health of enemy
var canFire : boolean = true; //switch that acts as a delay for AI shooting
var enemySpeed : float = 8; //speed at which enemy moves across the screen
var enemy1destroyed = false; //switch that helps us determin which enemy type was destroyed
var eBullet1 : Transform; //enemy bullet
var eWeaponPort1 :Transform; //spot where the enemy bullet comes out on enemy1
var nextFireTime = 0.0; //when this timer expires, will fire a bullet
var fireRate = 2.0; //controls how fast enemy can fire
function Awake()
{
}
function Start ()
{
}
function Update ()
{
transform.Translate(-enemySpeed*Time.deltaTime,0,0);
//if enemy moves off left side of screen...
if(transform.position.x <= -25)
{
//then have randomize its position and move it to the right side of the screen.
transform.position = Vector3(25* Time.deltaTime,Random.Range(-8,8),0);
}
//fire enemy1 gun routine
enemy1Fire();
//Destroy enemy when health reaches zero
if(health <= 0)
{
Destroy(gameObject);
//add some score to the points
var otherScript: SceneManager = GetComponent(SceneManager);
SceneManager.Addscore();
}
}
function OnTriggerEnter(other:Collider)
{
//check to see if enemy1 is colliding with player bullet
if(other.gameObject.tag == "bullet")
{
//subtract health when being hit by player bullet
health = health - 25;
}
}
function enemy1Fire()
{
//check to see if we can shoot, and if so, check to see if atleast a few seconds has passed
if(canFire && Time.time > nextFireTime)
{
//if time elapsed is greater than firing rate, fire weapon
nextFireTime = Time.time + fireRate;
//create the actual bullet
Instantiate(eBullet1,eWeaponPort1.position,eWeaponPort1.rotation);
canFire = true;
}
}
Any hints or suggestions on ways to go about solving this issue? Thanks
Answer by robertbu · Feb 26, 2013 at 10:57 PM
Here's an idea (assumes that eWeaponPort1 is tracking the player):
Put these at the top of the file. Since they are structs, we can store the data locally:
var qFireRotation : Quaternion;
var v3FirePosition : Vector3;
Merge in these two functions. Note the .25 is parameter is how much time between updates of the targeting data:
function Start() {
InvokeRepeating("UpdateFireData", 0.0, 0.25);
}
function UpdateFireData() {
qFireRotation = eWeaponPort1.rotation;
v3FirePosition = eWeaponPort1.position;
}
Rewrite your instantiate to:
Instantiate(eBullet1,v3FirePosition,qFireRotation);