- Home /
How To Make Enemy Shoot At You?
Hello, i want to make enemy shoot at me when it starts to follow me, im not sure but i think i should put the enemy shooting script in enemy AI script. What i am looking for is enemy shooting script, and where do i put it in enemy AI script. Enemy AI:
var distance;
var target : Transform;
var lookAtDistance = 15.0;
var attackRange = 10.0;
var moveSpeed = 5.0;
var damping = 6.0;
private var isItAttacking = false;
function Update ()
{
distance = Vector3.Distance(target.position, transform.position);
if(distance < lookAtDistance)
{
isItAttacking = false;
renderer.material.color = Color.yellow;
lookAt ();
}
if(distance > lookAtDistance)
{
renderer.material.color = Color.green;
}
if(distance < attackRange)
{
attack ();
}
if(isItAttacking)
{
renderer.material.color = Color.red;
}
}
function lookAt ()
{
var rotation = Quaternion.LookRotation(target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
}
function attack ()
{
isItAttacking = true;
renderer.material.color = Color.red;
transform.Translate(Vector3.forward * moveSpeed *Time.deltaTime);
}
I don't want an enemy shooting at me! First format your code better and maybe then someone will help you.
It's better. You should also remove the unnecessary white-spaces and use proper indentation. Doing this will make us happy!
I was about to say, it's now formatted, but still unreadable XD
Answer by DavidDebnar · Jul 08, 2013 at 02:05 PM
//Enemy.js
public var Target : Transform;
public var Projectile : Transform;
public var MaximumLookDistance : float = 15;
public var MaximumAttackDistance : float = 10;
public var FollowSpeed : float = 5;
public var MinimumDistanceFromPlayer : float = 2;
public var RotationDamping : float = 6;
function Update () {
var distance = Vector3.Distance(Target.position, transform.position);
if(distance <= MaximumLookDistance) {
LookAtTarget ();
if(distance <= MaximumAttackDistance)
AttackAndFollowTarget (distance);
}
else
renderer.material.color = Color.green;
}
function LookAtTarget () {
renderer.material.color = Color.yellow;
var dir = Target.position - transform.position;
dir.y = 0;
var rotation = Quaternion.LookRotation(dir);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
}
function AttackAndFollowTarget (distance : float) {
renderer.material.color = Color.red;
if(distance > MinimumDistanceFromPlayer)
transform.Translate((Target.position - transform.position).normalized * MoveSpeed * Time.deltaTime);
Instantiate(Projectile, transform.position + (Target.position - transform.position).normalized, Quaternion.LookRotation(Target.position - transform.position));
}
//Projectile.js
function Update () {
transform.Translate(transform.forward);
}
function OnCollisionEnter (c : Collision) {
//if(c.collider.CompareTag("Player"))
//c.collider.gameObject.GetComponent(PlayerScript).Damage();
Destroy(gameObject);
}
--David--
Answer by Nk.Andrei · Jul 08, 2013 at 01:23 PM
In function attack() if isItAttacking is true then instantiate a prefab (ex. a sphere) from the enemy position and add forward speed to your sphere or use a raycast...if the ray intersects your player then use instantiate again.
hey, i accidentaly messed something up, it still shoots but how do i make it shoot directly at me? this is the script now:
isItAttacking = true; clone = Instantiate(projectile, transform.position, transform.rotation);
i highly recommend you watch the brackys video on enemy AI.
Your answer
![](https://koobas.hobune.stream/wayback/20220613105653im_/https://answers.unity.com/themes/thub/images/avi.jpg)