Sentry Gun

Okay guys, I took the century gun from the FPS tutorial and adapted it to fit my needs. Here’s what I have. Here’s the standard “SentryGun.js” script on the weapon:

var attackRange = 30.0;
var shootAngleDistance = 10.0;
var target : Transform;

function Start () {
	if (target == null  GameObject.FindWithTag("Player"))
		target = GameObject.FindWithTag("Player").transform;
}

function Update () {
	if (target == null)
		return;
	
	if (!CanSeeTarget ())
		return;
	
	// Rotate towards target	
	var targetPoint = target.position;
	var targetRotation = Quaternion.LookRotation (targetPoint - transform.position, Vector3.up);
	transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 2.0);

	// If we are almost rotated towards target - fire one clip of ammo
	var forward = transform.TransformDirection(Vector3.forward);
	var targetDir = target.position - transform.position;
	if (Vector3.Angle(forward, targetDir) < shootAngleDistance)
		SendMessage("Fire");
}

function CanSeeTarget () : boolean
{
	if (Vector3.Distance(transform.position, target.position) > attackRange)
		return false;
		
	var hit : RaycastHit;
	if (Physics.Linecast (transform.position, target.position, hit))
		return hit.transform == target;

	return false;
}

I didn’t change anything there. However, I think I might have to. Here’s what I have to trigger the “Fire Function”:

var range = 100.0;
var timer = 1.0;
var missile : GameObject;

function Fire () {
	yield new WaitForSeconds (timer);
	var position : Vector3 = new Vector3(0, 0, 0) * 1.0;
		position = transform.TransformPoint (position);
		var thisMissile : GameObject = Instantiate (missile, position, transform.rotation) as GameObject;
		Physics.IgnoreCollision(thisMissile.collider, collider);
}

Now, as you might be able to tell, I am trying to separate the time in-between shots. (hence the reason I put WaitForSeconds in there…however, I realized that it doesn’t repeat the function so the WaitForSeconds isn’t called in-between each shot…can someone help me out so that there is a one second pause in-between each shot?

1 Like

attach the machinegun script, and that has an easy delay you can customize