Helicopter AI Script

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();
		}
	}
}

HC_Positions is never called?

true, just tryed it then, but it still doesn’t work… Well i updated the question

bump… really need a answer to this as i cant find any tutorials, or any other questions about this…

Here is the link for i ever seen Helicopter tutorial in unity3d with real physics.

http://http://www.gotow.net/andrew/wordpress/?page_id=99

i have been through it, already, but it is for some one to operate the helicopter. 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.

Surely you could use the helicopter tutorial, then add some AI to control the settings rather than the player?

Or you could be looking for something more like http://wiki.unity3d.com/index.php/SeekSteer - this just follows waypoints, but you could use a linecast to check whether there’s something between it and the player. I’d use one enormous ‘if’ construct - if it can see the player, then move towards it, otherwise follow the waypoint code I linked to before.

I did have a very simple helicopter AI code (in JS), but all it did was follow the player. If it would be useful then I’ll post it.

Oh, and I’ve never played CoD, so I’m not entirely sure what you’re trying to achieve…

thank you this will help me immensely!

Not perfect by a long chalk, it was one of the first things I wrote. The variables are almost certainly messed up, and the helicopter does weird things when above the player. I’ll have a go at re-writing it now, to see if I can get it to do what you want… :wink:

Oh, this also covers firing weapons, so if you have a script attached to the chopper that has a function “Fire”, that will get triggered every so often.

var target : Transform;
var forwardForce = 5.0;
var throttle = 0.0;
var minimumThrottle = 0.0;
var maximumThrottle = 100.0;
var heightDistance = 0.0;
var heightReaction = 10.0;
var turnSpeed = 6.0;
var accuracy = 10.0;
var minAltitude = 20.0;
var safeDistance = 20.0;
var timerMin = 1.0;
private var distanceToGround = 0.0;
private var distanceAhead = 0.0;
private var timer = 0.0;
private var startTime = 0.0;

function Start () {
	startTime = Time.time;
}

function FixedUpdate () {
	if(!target)
		if(GameObject.FindWithTag("Player"))
			target = GameObject.FindWithTag("Player").transform;
		else
			return;
	if(target) {
		// Timer
		timer = Time.time - startTime;
		heightDistance = (target.position-transform.position).y;
		var hit : RaycastHit;
		if (Physics.Raycast (transform.position, transform.forward, hit))
			distanceAhead = hit.distance;
		if (Physics.Raycast (transform.position, -Vector3.up, hit))
			distanceToGround = hit.distance;
		// If there's nothing ahead of us...
		if(distanceAhead > safeDistance) {
			rigidbody.AddForce(transform.forward*forwardForce);
		} else {
			rigidbody.AddForce(-transform.forward*forwardForce);
		}
		// Choose whether to go up or down
		if (distanceToGround > minAltitude)
			throttle = Mathf.Lerp(minimumThrottle, maximumThrottle,((heightDistance-minAltitude)/heightReaction));
		else
			throttle = Mathf.Lerp(throttle, maximumThrottle, (Time.deltaTime*heightReaction));
		// Make the throttle happen
		rigidbody.AddForce(Vector3.up*throttle);
		// The 'gunner' part of the script
		var predictedTargetPosition = target.position;
		var relativePos = predictedTargetPosition - transform.position;
		var rotation = Quaternion.LookRotation(relativePos);
		transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime*turnSpeed);
		var targetDirection = target.position - transform.position;
		if(Vector3.Angle(targetDirection, transform.forward) < accuracy)
			if(timer > timerMin) {
				BroadcastMessage("Fire", target, SendMessageOptions.DontRequireReceiver);
				startTime = Time.time;
			}
	}
}