- Home /
Question by
deadmaggot1 · Apr 02, 2015 at 06:08 PM ·
scripting beginner
Melee & range 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. i havent been able to fine anything online to help me with this. I'm still quite new to this so any help would be amazing.
the script is:
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.<Rigidbody>().AddForce(Bullet.transform.forward * BulletSpeed);
Debug.Log("Shoot");
}
Comment