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);
}
//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–
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.