My script is for a automated helicopter that moves to different places if it cannot see the player, and once it does see the player it fires bullets at the player. You can think of it kind of like the helicopter in Call of Duty… but nothing is happening, even if the helicopter is not moving anywhere, even if i take out the part where it is supposed to look at the player, it still does nothing and stays in the same position. if anyone can help i thank you. can the answer preferably be in c# (but i can convert from JS if need be).
using UnityEngine;
using System.Collections;
public class HelicopterAI : MonoBehaviour {
public Vector3[] HCPatrolPositions;
public bool SeePlayer;
public Transform Helicopter, Player;
public Camera cam;
public int NoOfHCPatrolPos;
public float StayPosMaxTime;
public Vector3 NewHCPosition;
public float LookAtPlayer_Speed, LookAtNextPos_Speed;
//
private float StayPosTimer;
private Plane[] planes;
private int RandomPosIndex;
private float LookAtPlayer_x, LookAtPlayer_y, LookAtPlayer_z;
private float LookAtNextPos_x, LookAtNextPos_y, LookAtNextPos_z;
private Vector3 LookAtPos_Player, LookAtPos_NextPos;
void Start ()
{
StayPosMaxTime = 4;
NoOfHCPatrolPos = 3;
LookAtPlayer_Speed = 10;
LookAtNextPos_Speed = 10;
planes = GeometryUtility.CalculateFrustumPlanes(cam);
SeePlayer = false;
HC_Positions();
}
void Update ()
{
Detection();
HC_Smooth_Look_At_Player();
HC_Smooth_Look_At_NextPos();
HC_Position_Timer();
HC_Position_data();
StayPosTimer += Time.deltaTime;
}
void HC_Positions ()
{
HCPatrolPositions[0] = new Vector3(100, 50, 100);
HCPatrolPositions[1] = new Vector3(100, 50, 0);
HCPatrolPositions[2] = new Vector3(0, 50, 100);
HCPatrolPositions[3] = new Vector3(0, 50, 0);
}
void HC_Positions_Select ()
{
RandomPosIndex = Random.Range(0, NoOfHCPatrolPos);
NewHCPosition = HCPatrolPositions[RandomPosIndex];
}
void Detection ()
{
if (GeometryUtility.TestPlanesAABB(planes, Player.collider.bounds))
{
Debug.Log(Player.name + " has been detected!");
SeePlayer = true;
}
else
{
Debug.Log("Nothing has been detected");
}
}
void HC_Position_data ()
{
if (Helicopter.gameObject.activeInHierarchy)
{
if (Helicopter.position != NewHCPosition StayPosTimer >= StayPosMaxTime)
{
Helicopter.Translate(NewHCPosition);
Helicopter.LookAt(LookAtPos_NextPos);
}
while (SeePlayer == true)
{
Helicopter.LookAt(LookAtPos_Player);
}
}
}
void Player_Is_Seen ()
{
while (SeePlayer == true)
{
Helicopter.LookAt(LookAtPos_Player);
}
}
void HC_Smooth_Look_At_Player ()
{
LookAtPlayer_x = Mathf.SmoothDampAngle(transform.eulerAngles.x, Player.eulerAngles.x, ref LookAtPlayer_x, LookAtPlayer_Speed);
LookAtPlayer_y = Mathf.SmoothDampAngle(transform.eulerAngles.y, Player.eulerAngles.y, ref LookAtPlayer_y, LookAtPlayer_Speed);
LookAtPlayer_z = Mathf.SmoothDampAngle(transform.eulerAngles.z, Player.eulerAngles.z, ref LookAtPlayer_z, LookAtPlayer_Speed);
LookAtPos_Player = new Vector3(LookAtPlayer_x, LookAtPlayer_y, LookAtPlayer_z);
}
void HC_Smooth_Look_At_NextPos ()
{
LookAtNextPos_x = Mathf.SmoothDampAngle(transform.eulerAngles.x, Player.eulerAngles.x, ref LookAtNextPos_x, LookAtNextPos_Speed);
LookAtNextPos_y = Mathf.SmoothDampAngle(transform.eulerAngles.y, Player.eulerAngles.y, ref LookAtNextPos_y, LookAtNextPos_Speed);
LookAtNextPos_z = Mathf.SmoothDampAngle(transform.eulerAngles.z, Player.eulerAngles.z, ref LookAtNextPos_z, LookAtNextPos_Speed);
LookAtPos_NextPos = new Vector3(LookAtNextPos_x, LookAtNextPos_y, LookAtNextPos_z);
}
void HC_Position_Timer ()
{
if (SeePlayer == true)
{
StayPosMaxTime = Mathf.Infinity;
}
else if (Helicopter.position == NewHCPosition)
{
StayPosTimer = 0;
HC_Positions_Select();
}
}
}