- Home /
Enemy AI i have this script for a ranged ai unit but i need it to use a melee (bite) attack when the player is close enough.
var runSpeed = 3; var biteRange = 3; var walkSpeed = 1; var rotationSpeed = 3; var attackRange = 3; var chaseRange = 10; var giveUpRange = 20; var dontComeCloserRange = 2; var attackRepeatTime = 0.9; var controller : CharacterController; public var target : Transform; var biteAnim : String; var idleAnim : String; var idleaimAnim : String; var idleaimhighAnim : String; var idleaimlowAnim : String; var runAnim : String; var walkAnim : String; var shootAnim : String; private var initialSpeed = 0; var chasing = false; private var attackTime = 1; var myTransform : Transform; var PauseRunning = false; var Anim : Animation; var lookStrength = 2; var option = false; var option2 = false; var Proj : Transform; var MuzzleFlash : Transform; var BulletSpeed = 20; var BulletPos : Transform; private var idleaimAnimReal; var AudSource : AudioSource; private var currentMuzzle;
function Awake() {
myTransform = transform;
}
function Start() { Anim.Play(idleAnim);
target = GameObject.FindWithTag("Player").transform;
}
function Update () { Debug.DrawLine( target.position , BulletPos.position , Color.cyan); var distance = (target.position - BulletPos.position).magnitude;
if ( target.position.y == transform.position.y ) {
idleaimAnimReal = idleaimAnim;
}
if ( target.position.y > transform.position.y ) {
idleaimAnimReal = idleaimhighAnim;
}
if ( target.position.y < transform.position.y ) {
idleaimAnimReal = idleaimlowAnim;
}
BulletPos.LookAt(target);
if (distance < chaseRange) {
chasing = true;
}
if (chasing) {
if ( PauseRunning == false ) {
targetRot = new Vector3(target.position.x, this.transform.position.y, target.position.z);
transform.LookAt(targetRot);
controller.SimpleMove(runSpeed*transform.forward);
Anim.Play(runAnim);
}
}
else
{
Anim.Play(idleAnim);
}
if (distance > giveUpRange) {
chasing = false;
}
if (distance < biteRange) {
Anim.Play(biteAnim);
attackRepeatTime = 0;
}
if (distance < dontComeCloserRange) {
// targetRotation = Quaternion.LookRotation (target.position - transform.position); //
// str = Mathf.Min (lookStrength * Time.deltaTime, 1);
targetRot = new Vector3(target.position.x, this.transform.position.y, target.position.z);
// transform.rotation.z = Quaternion.Lerp (transform.rotation, targetRotation, str); transform.LookAt(targetRot); PauseRunning = true; if ( option == false ) { InvokeRepeating("Fire", 0, attackRepeatTime); option = true; dontComeCloserRange += 5; option2 = true; } } if (distance > dontComeCloserRange) { PauseRunning = false; CancelInvoke(); option = false; if ( option2 == true ) { dontComeCloserRange -= 5; option2 = false; } }
if ( PauseRunning == true ) {
Anim.Play(idleaimAnim);
}
}
function Fire () {
Anim.Play(shootAnim); attackRepeatTime = 2; Debug.Log("Shoot1"); var Bullet : Transform; var Muzzle : GameObject; AudSource.Play(); Bullet = Instantiate(Proj, BulletPos.position, BulletPos.rotation); Muzzle = Instantiate(MuzzleFlash, BulletPos.position, BulletPos.rotation).gameObject; Bullet.GetComponent.().AddForce(Bullet.transform.forward * BulletSpeed); Debug.Log("Shoot");
}
Answer by MerpysoupStudio · Aug 24, 2020 at 08:07 PM
@gs10484 You can put this stuff in the update method, but it is somewhat expensive so if your game needs to be optimized you might want to try something other than vector3.distance.
distanceFromPlayer = Vector3.Distance(transform.position, target.position);
if(distanceFromPlayer <= minimumDistance)
{
Bite();
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613010425im_/https://answers.unity.com/themes/thub/images/avi.jpg)