Turret Automatic Aiming

Hi guys, can you tell me what i can do to this piece of java script so that the turrets which fire the bullets fire automatically and only when your character is in site. So far the turret aims at my player and shoots at my player when i press space bar.

 // Instantiates a projectile every 0.1 seconds,
// if the Fire1 button is pressed or held down.
var projectile : Rigidbody;
var fireRate = 0.5;

var Velocity : float = 25;

private var nextFire = 0.0;

function Update () {

    //If the player holds down or presses the left mouse button
    if (Input.GetButton ("Jump") && Time.time > nextFire) {


//To make our scripting a little more Object-Oriented-Programming, we will create our custom functions as well
function Shoot() {

    //Add fireRate and current time to nextFire
    nextFire = Time.time + fireRate;

    //Instantiate the projectile
    clone = Instantiate (projectile, transform.position, transform.rotation);

    //Name the clone "Shot" ::: this name will appear in the Hierarchy View when you Instantiate the object
    clone.name = "Shot";

    //Add speed to the target
    clone.rigidbody.velocity = transform.TransformDirection (Vector3.forward * Velocity);


Thanks, Easy points

You could do a raycast at the player to see if anything is in the way before firing the gun.

I assume you have another "LookAt" script or something that is doing your automatic aiming and that you're target GameObject is called "target".

here is an updated Update:

function Update () {
    if (Time.time >= nextFire) {
        var hit:RaycastHit;
        var visible = true;
        if (Physics.Raycast(transform.position, transform.forward, hit, 
            Vector3.Distance(transform.position, target.transform.position))) {

            // Check that the raycast hit something other than our target.
            if (hit.transform.gameObject != target)
                visible = false;

        if (visible) {
            // Fire our gun etc.
            nextFire = Time.time + fireRate;

            clone = Instantiate ...