Maybe you guys can help, but I have no idea…
Both scripts are attached to a sphere which pivots correctly towards the target, fires correctly, and auto-adjusts correctly. However, I just want it to shoot one bullet every so often. Currently, it fires constantly (until it needs to re-adjust).
SentryGun.js:
var attackRange = 30.0;
var shootAngleDistance = 10.0;
var target : Transform;
var timer = 0.5;
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 * 4.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;
}
MachineGun.js:
var range = 100.0;
var timer = 0.5;
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);
}
BulletForce.js (attached to the bullet):
var explosion : GameObject;
var LevelDeath : int = 0;
var Speed : int = 1000.0;
function FixedUpdate () {
rigidbody.AddForce (transform.TransformDirection (Vector3.forward) * Speed);
}
function OnCollisionEnter(collision : Collision) {
var contact : ContactPoint = collision.contacts[0];
if (collision.gameObject.tag == "Player")
{
Destroy (gameObject);
Destroy (collision.gameObject);
}
if (collision.gameObject.tag == "Wall")
{
Destroy (gameObject);
}
if (collision.gameObject.tag == "Destructable")
{
Destroy (collision.gameObject);
Destroy (gameObject);
}
}