getting my enemy to shoot at me : trying to work it out

Hi Guys,

I have an ememy that uses the lookat() function to follow me which is not a problem, but i am having trouble o n how to make the enemy shoot at me when it gets to the minimum distance and was wondering if you could help or has someone got a script i could use so i could understand it a bit better, i am aware that it would be along a transform but i do not know how to execute it, any feedback would be great: :wink: p.s i have checked the forums and nothing quite explainms it properly

var Player : Transform; // The target
var EnemSpeed = 2.0; // speed of the enemy
var MaxDist = 10;
var MinDist = 5;
var bullet : Transform;

function Update ()
{

transform.LookAt(Player); // when player get close the enemy will follow the player

if(Vector3.Distance(transform.position, Player.position) >= MinDist){

transform.position += transform.forwardEnemSpeedTime.deltaTime;

}
}
// now shoot at player

You can use overlap sphere cast to see if a player is in the Radius of the enemy… From that you could check to see if the player is in Viewable state, Like the 180degree’s of the Enemies view… Then do the Look at player.

public float range = 5;
public float viewArc = 180;
private Collider[] hit;  //All Colliders that are hit with overlap sphere
public Transform holderCenter;  //Whatever center position you want it to be.

void Awake()
{
	holderCenter = transform;
}

public bool CanSeePlayer(float distance, float angle)
{
        //Player is on Layer 10. So we only look for that layer.
	hit = Physics.OverlapSphere(holderCenter.position, distance, 1<<10);
        foreach (Collider c in hit)
	{ 
            if (Vector3.Angle(holderCenter.forward, c.transform.position - holderCenter.position) <= angle)
            {
			return true;
	    }
	}
	return false;
}

Hi Ereous,

Thank you for the response do you have this in Javascript as i am not to good with C# it would be a great help and i have never used layers

Kind regards

Dav id

okay i went away again and have created an empty gameobject and named it spawnpoint, i have put this near my enemy just in front so spawnpoint and bullet are children of enemy. This is my code:

var LookAtTarget : Transform; // look at target
var damp = 1.0; // made up number
var bullet : Transform;

function Update ()
{
if(LookAtTarget) // if true or false
{
var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position); // vector3 vars
transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp); // current rotation, rotate var, time that we specify

Shoot();
}
// transform.LookAt(LookAtTarget); // look at the player

}

function Shoot() // enemy to shoot
{
var bullet = Instantiate(bullet, transform.Find(“spawnpoint”).transform.position, Quaternion.identity);

bullet.rigidbody.AddForce(transform.forward * 100);

}
in the inspector when i chose enemy the lookat is looking at player so this working, i have dragged bullet(which is a prefab) on bullet in the inspector,when i play the game the bullet does not shoot and just sits on the ground rotating instead of shooting which i cannot understand, could someone please help me to see where i am going wrong please, i am trying my best but it is not working

ps. the bullet is attached to spwanpoint but also have tried attaching it to enemy to no avail