Hi.
I’ll be simple.
Here is my script source of what i’m building for the enemy. (If someone see somethink which i can optimize. feel free to say)
using UnityEngine;
using System.Collections;
// ENEMY SCRIPT
public class Enemy_Script : MonoBehaviour {
// inspector variables
public float Enemy_Speed = 8f; // ship speed
public float Enemy_Rotation_Speed = 150f; // ship rotation speed
private Transform Target; // what this will follow to kill
public Transform Bullet; // what bullet the ship will shoot
public Transform[] Bullet_Sockets; // how many weapon places the ship have
private int inEnergy = 0;
// ingame stats
public int Energy = 100; // life and everythink of the ship.
public float Energy_CoolDown = 0.1f; // hw long will wait to refill the energy
public int Bullet_Energy_Cost = 1; // hw much will spend on energy every shoot
public float Bullet_CoolDown = 0.5f; // hw long will wait to shoot again
public int Rocket_Energy_Cost = 8; // hw much will spend on energy every launch
public float Rocket_CoolDown = 1.0f; // hw long will wait to launch again
private bool CanShoot = true; // stop until coroutine done
//private bool CanLaunch = true; // stop until coroutine done
private bool Recharge = true; // stop until coroutine done
IEnumerator ShootTimer() { // shoot timer
yield return new WaitForSeconds(Bullet_CoolDown);
CanShoot = true;
}
//IEnumerator LaunchTimer() { // shoot timer
// yield return new WaitForSeconds(Rocket_CoolDown);
// CanLaunch = true;
//}
IEnumerator EnergyTimer() { // energy timer
yield return new WaitForSeconds(Energy_CoolDown);
if (inEnergy < Energy) inEnergy++; // increment +1 energy every 0.1 seconds
Recharge = true;
}
// Use this for initialization
void Start () {
Target = GameObject.FindWithTag("Player_Ship").transform;
inEnergy = Energy;
}
private void RedefinePosAng() {
transform.position = new Vector3(transform.position.x,
transform.position.y,
0);
}
// Update is called once per frame
void Update () {
// turn the enemy to be facing the target
Vector3 TargetPos = transform.InverseTransformPoint(Target.position);
TargetPos.y = 0;
// get the angle to reach the target
float angle = Vector3.Angle(TargetPos, transform.forward);
float lr = Mathf.Ceil(Vector3.Dot(Vector3.right, TargetPos.normalized)); // get a dot between the axis
float amount = Enemy_Rotation_Speed * Time.deltaTime;
if (amount > angle) amount = angle; //if rotation higher than the angle, use the angle
if (lr == 1) amount = -amount; // check which side the ship should turn
transform.Rotate(0, 0, amount); // rotate the ship
transform.Translate(0, Time.deltaTime * Enemy_Speed, 0); // move the ship
//RedefinePosAng();
print(amount);
// the energy must be equal or more than the energy cost, and must be able to shoot
if (inEnergy >= Bullet_Energy_Cost CanShoot) {
// get every bullet-socket
for (int i = 0; i <= Bullet_Sockets.Length-1; i++) {
print(i);
// instantiate the bullet in socket.
GameObject esBullet = Instantiate(Bullet, Bullet_Sockets[i].position, Bullet_Sockets[i].rotation) as GameObject;
Bullet_Script bs = esBullet.GetComponent<Bullet_Script>();
bs.Owner = this.transform;
}
inEnergy -= Bullet_Energy_Cost; // decreasy energy from bullet_Cost
CanShoot = false; // disable shoot while cooldowntimer ends.
StartCoroutine(ShootTimer());
}
if (Recharge) {
Recharge = false;
StartCoroutine(EnergyTimer());
}
}
void OnGUI() {
GUI.Label(new Rect(5,
65, 100, 20), "Energy: "+inEnergy);
}
}
Here my Model:
the two points in front of the ship, there are 2 sockets for bullets there. When i run, the ship only shoot from socket[0]. The console returns:
Someone can help me identify where i’m missing!? somethink is wrong, and probably causing erro on instantiated bullet from socket[1]. Looks like the bullet on socket [0] always work but when the index goes to 1… the erro shows up.
Best regards.