well… never seen this before… trying to do damage to other ships with Raycast
snippet:
if (dist < 500 && IsBlueTeamFighter == true) {
MachineGuns.SetActive (true);
RaycastHit hit = new RaycastHit ();
if (Physics.Raycast (transform.position, fwd, 500))
if (Hit.collider.tag == "Red_Team_fighter") {
if(FireRate < 0.01){ // damage from mach guns limited by firea rate
Hit.transform.gameObject.GetComponent(HealthDamage).IsHit = true;
Hit.transform.gameObject.GetComponent(HealthDamage).damage = 1;
Instantiate(BulletHit, hit.point, Quaternion.identity);
}
}
if (dist < 500 && IsBlueTeamFighter == false) {
MachineGuns.SetActive (true);
RaycastHit hit = new RaycastHit ();
if (Physics.Raycast (transform.position, fwd, 500))
if (Hit.collider.tag == "Blue_Team_fighter") {
if(FireRate < 0.01){ // damage from mach guns limited by firea rate
Hit.transform.gameObject.GetComponent(HealthDamage).IsHit = true;
Hit.transform.gameObject.GetComponent(HealthDamage).damage = 1;
Instantiate(BulletHit, hit.point, Quaternion.identity);
}
}
}
}
full script
using UnityEngine;
using System.Collections;
public class AIFighter : MonoBehaviour {
private int index;
public float rotationSpeed = 12f;
private float MaxAttackingDistance = 300;
private float MaxLockonDistance = 2000;
private float dist;
private float blueFighterHP = 25;
public float FireRate = 0.03f;
public bool IsBlueTeamFighter;
public bool rotating;
public bool Attacking = true;
public bool LowFule;
public bool retreating;
public bool Refuling;
public GameObject target;
public GameObject[] Targets;
public GameObject MachineGuns;
public GameObject BulletHit;
public RaycastHit Hit;
public float FighterFule = 10000;
public void Start (){
if (IsBlueTeamFighter == true) {
Targets = GameObject.FindGameObjectsWithTag ("RedTeam_Fighter");
index = Random.Range (0, Targets.Length);
target = Targets [index];
if (target = null) {
Targets = GameObject.FindGameObjectsWithTag ("RedTeam_Fighter");
index = Random.Range (0, Targets.Length);
target = Targets [index];
}
}
else
{
if (IsBlueTeamFighter == false) {
Targets = GameObject.FindGameObjectsWithTag ("BlueTeam_Fighter");
index = Random.Range (0, Targets.Length);
target = Targets [index];
if (target = null) {
Targets = GameObject.FindGameObjectsWithTag ("BlueTeam_Fighter");
index = Random.Range (0, Targets.Length);
target = Targets [index];
}
}
}
}
// END START___________________________________________________________________________________________________________
//______________________________________________________________________________________________________________
void LateUpdate () {
//______________________________________________________________________________________________________________
// Move Forward
FireRate -= 0.01f;
if(FireRate <0.01f){ FireRate = 0.3f;}
FighterFule -= 1 * Time.deltaTime;
//______________________________________________________________________________________________________________
//______________________________________________________________________________________________________________
if (dist > MaxLockonDistance) {
target = null;
Attacking = false;
rotating = false;
}
//______________________________________________________________________________________________________________
//______________________________________________________________________________________________________________
if(retreating == true){
transform.rotation = Quaternion.Slerp (target.transform.rotation, Quaternion.LookRotation (target.transform.position - transform.position), Time.deltaTime * rotationSpeed);
}
if (Refuling == true) {
retreating = false;
FighterFule += 100 * Time.deltaTime;
if(FighterFule == 10000){
Refuling = false;
Attacking = true;
}
}
//______________________________________________________________________________________________________________
//______________________________________________________________________________________________________________
if (Attacking == true) {
// Rotate to look at Target;
transform.rotation = Quaternion.Slerp (target.transform.rotation, Quaternion.LookRotation (target.transform.position - transform.position), Time.deltaTime * rotationSpeed);
}
//______________________________________________________________________________________________________________
//______________________________________________________________________________________________________________
// get the distance to target
dist = Vector3.Distance (target.transform.position, transform.position);
Vector3 fwd = transform.TransformDirection(Vector3.forward); //the direction the fighter is facing
//______________________________________________________________________________________________________________
//______________________________________________________________________________________________________________
if (dist < 500 && IsBlueTeamFighter == true) {
MachineGuns.SetActive (true);
RaycastHit hit = new RaycastHit ();
if (Physics.Raycast (transform.position, fwd, 500))
if (Hit.collider.tag == "Red_Team_fighter") {
if(FireRate < 0.01){ // damage from mach guns limited by firea rate
Hit.transform.gameObject.GetComponent(HealthDamage).IsHit = true;
Hit.transform.gameObject.GetComponent(HealthDamage).damage = 1;
Instantiate(BulletHit, hit.point, Quaternion.identity);
}
}
if (dist < 500 && IsBlueTeamFighter == false) {
MachineGuns.SetActive (true);
RaycastHit hit = new RaycastHit ();
if (Physics.Raycast (transform.position, fwd, 500))
if (Hit.collider.tag == "Blue_Team_fighter") {
if(FireRate < 0.01){ // damage from mach guns limited by firea rate
Hit.transform.gameObject.GetComponent(HealthDamage).IsHit = true;
Hit.transform.gameObject.GetComponent(HealthDamage).damage = 1;
Instantiate(BulletHit, hit.point, Quaternion.identity);
}
}
}
}
//______________________________________________________________________________________________________________
//______________________________________________________________________________________________________________
if(dist > 500) {
MachineGuns.SetActive (false);
}
//______________________________________________________________________________________________________________
//______________________________________________________________________________________________________________
if (dist > MaxAttackingDistance) {
Attacking = true;
rotating = false;
//Re-Aquire Target
} else {
Attacking = false;
rotating = true;
}
//______________________________________________________________________________________________________________
//____Avoid when close___________________________________________________________________________________________
if (rotating == true) {
// AVOID OTHER FIGHTER ?
}
//______________________________________________________________________________________________________________
//___Fule_______________________________________________________________________________________________
if (FighterFule < 3000 && IsBlueTeamFighter == true) {
Targets = GameObject.FindGameObjectsWithTag ("BlueTeam_CarrierDock");
index = Random.Range (0, Targets.Length);
target = Targets [index];
if (dist > 6000) { target = null; }
retreating = true;
}
if (FighterFule < 3000 && IsBlueTeamFighter == true) {
Targets = GameObject.FindGameObjectsWithTag ("BlueTeam_CarrierDock");
index = Random.Range (0, Targets.Length);
target = Targets [index];
if (dist > 6000) { target = null; }
retreating = true;
}
//______________________________________________________________________________________________________________
} // END LATEUPDATE____________________________________________________________________________________________
//______________________________________________________________________________________________________________
}