Enemy AI - firing (instantiate) bullets on finding the player

I need to create a script that will allow the enemies in my scene to fire a sphere at the player, once they have detected him. so far I have the code for the Enemy detecting the players presence if they are within a certain range. however i’d appreciate the help with this as i’m completely new to scripting

thanks!

var LookAtTarget : Transform;
var range = 100.0;
function Update()
{
	if (InAttackRange())
	{
		var targetRotation = Quaternion.LookRotation (LookAtTarget.position - transform.position, Vector3.up);
	transform.rotation = Quaternion.Slerp (transform.rotation, targetRotation, Time.deltaTime * 2.0);
	}
}
function InAttackRange()
{
	if(Vector3.Distance (transform.position, LookAtTarget.position) > range)
	{
		return false;
	}
	var hit : RaycastHit;
	if (Physics.Linecast (transform.position, LookAtTarget.position, hit))
	{
		if (hit.collider.gameObject.tag != "Player")
		{
			return false;
		}
	}
	return true;
}

OK so I’ve got this code so far, however this is giving me an error message

“Assets/Scripts/EnemyTarget.js(21,33): BCE0023: No appropriate version of ‘UnityEngine.Object.Instantiate’ for the argument list ‘(UnityEngine.Transform, void)’ was found.”

var LookAtTarget : Transform;
var range = 100.0;
var newObject : Transform;
var fl_last_shot : float;

function Update()
{
	if (InAttackRange())
	{
		var targetRotation = Quaternion.LookRotation (LookAtTarget.position - transform.position, Vector3.up);
	transform.rotation = Quaternion.Slerp (transform.rotation, targetRotation, Time.deltaTime * 2.0);
	}
}
function Shoot(seconds) 
{      
     if (fl_last_shot + 2 < Time.time) {
        Shoot(2);
        fl_last_shot = Time.time;
    }
	//transform.LookAt(Target); 
	var bullet = Instantiate(newObject, transform.LookAt(LookAtTarget));
Physics.IgnoreCollision(newObject.collider, transform.root.collider);			
  bullet.rigidbody.AddForce(transform.forward  * 10); 
   
   }
function InAttackRange()
{
	if(Vector3.Distance (transform.position, LookAtTarget.position) > range)
	{
		return false;
	}
	var hit : RaycastHit;
	if (Physics.Linecast (transform.position, LookAtTarget.position, hit))
	{
		if (hit.collider.gameObject.tag != "Player")
		{
			return false;
		}
	}
	return true;
	
}

should they ever stop? lookattarget and a raycast sounds not bad for this job…
i would go this way:

function OnTriggerStay (hit : Collider){
	if(hit.gameObject.tag == "Player"){
		hit.SendMessage("StartShooting");
	}
}
////////////////////
var projectilePrefab:Transform;
var Spawnpoint:GameObject;
private var Speed: float = 2;
private var ray: Ray;
private var targetRange : float = 150.0;
private var rayHit : RaycastHit;
private var speedUpRotation: float = 2;

function StartShooting(){
var Target : GameObject[];
Target = GameObject.FindGameObjectsWithTag("Player");
MyTarget = Target[0];

var rotateTo = Quaternion.LookRotation(MyTarget.transform.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotateTo, Time.deltaTime*speedUpRotation);

var hit : RaycastHit;
if(Physics.Raycast(ray, rayHit,targetRange)  rayHit.collider.gameObject.tag == "Player"){
var projectile = Instantiate(projectilePrefab, Spawnpoint.transform.position, transform.rotation);
projectile.rigidbody.AddForce(transform.forward, Speed);
		}
}

this is UNTESTED… just paste some things together which could be used for that what you want to achiev and you should use a timer for the Instantiate part.

should the bullet behave like a heat seeking missile and chase the target?

thanks for helping :slight_smile:

well it doesn’t work so far, however there are no error messages ha

ye i was hoping that once the player left the attack range of the enemy they would stop firing

do i set spawn point as the enemy game object? (sorry if this is noobens barrichello)

edit:
the bullet should fire in a simple straight line shot

no problem, bro - i´m a noob to… getting deeper and deeper into the whole scripting thing i realize that there´s much to learn;)

yes, you need an object as spawnpoint…an empty gameobject for example. place it where your bullet should spawn and link it in the inspector. no spawnpoint, no bullet;) well…i´ll go throught the code i´ve posted and make it work, if you don´t know how to use it

that’d be great cheers man!

ye i’ve got a long way to go ha

give me 10 min

This is what I’ve come up with so far. . . no errors and the enemy will look at the character whilst they’re in range but won’t fire anything towards them. I’ll try to get that spawn points script bit in there maybe that will do it?

var LookAtTarget : Transform;
var range = 100.0;
var damp = 5.0;
var bullet : Transform;
var savedTime = 0;

function Update()
{
	if(InAttackRange())
	{
		var targetRotation = Quaternion.LookRotation (LookAtTarget.position - transform.position, Vector3.up);
	transform.rotation = Quaternion.Slerp (transform.rotation, targetRotation, Time.deltaTime * damp);
	}
}
function InAttackRange()
{
	if(Vector3.Distance (transform.position, LookAtTarget.position) > range)
	{
		return false;
	}
	var hit : RaycastHit;
	if (Physics.Linecast (transform.position, LookAtTarget.position, hit))
	{
		if (hit.collider.gameObject.tag != "Player")
		{
			return false;
		}
	}
	return true;
	
}

var seconds : int = Time.time;
var oddeven = (seconds % 2);

if(oddeven)
{
Shoot(seconds);
}
function Shoot(seconds)
{
if(seconds!=savedTime)
{
var bullet = Instantiate(bullet ,transform.Find("BulletSpawn").transform.position ,Quaternion.identity);

bullet.rigidbody.AddForce(transform.forward * 10);
savedTime=seconds;
}

}

well i´m not sure…something is not working in my example…maybe im to tired …zzzz

last try

var LookAtTarget : Transform;
var range = 100.0;
var damp = 5.0;
var bulletPrefab : Transform;
var savedTime = 0;
var nextShotTime : float = 0.0;
var timeBetweenShots : float = 1.0;
var randomizer:float= 1;
function Update()
{
	if(Vector3.Distance (transform.position, LookAtTarget.position) > range){
		//InAttackRange()
		var targetRotation = Quaternion.LookRotation (LookAtTarget.position - transform.position, Vector3.up);
		transform.rotation = Quaternion.Slerp (transform.rotation, targetRotation, Time.deltaTime * damp);
		
		if (nextShotTime <= Time.time + randomizer){
				Shoot();
				nextShotTime = Time.time + timeBetweenShots + randomizer/2;
		}
	}
}
/*
function InAttackRange()
{
	var hit : RaycastHit;
	if (Physics.RaycastHit (transform.position, LookAtTarget.position, hit)  hit.collider.gameObject.tag == "Player"){
			Shoot(seconds);
	}
}
*/
function Shoot()
{	
		var bullet = Instantiate(bulletPrefab, transform.Find("SpawnPoint").transform.position,transform.rotation);
		bullet.rigidbody.AddForce(transform.forward * speed);
		randomizer = Random.Range(0, 0.9)/Random.Range(0.45,1.25);
}

argh i can;t get it to work. . . i think my brain might explode.

thanks for trying anyways