making a turet shoot

so i downloaded the fps add on put a turret down and it wouldnt fire at me it only looked at me and the muzzle flash was still there so since im not the best scripter around i thought id use the machine gun script to edit it and made this:

var attackRange = 30.0; var shootAngleDistance = 10.0; var target : Transform; var damage = 5.0; var fireRate = 0.05; var force = 10.0; private var hitParticles : ParticleEmitter;

function Start () { if (target == null && GameObject.FindWithTag("Player")) target = GameObject.FindWithTag("Player").transform; hitParticles = GetComponentInChildren(ParticleEmitter);

// We don't want to emit particles all the time, only when we hit something.
if (hitParticles)
    hitParticles.emit = false;


function Update () { if (target == null) return;

if (!CanSeeTarget ())

// 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)


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;


function FireOneShot () { var direction = transform.TransformDirection(Vector3.forward); var hit : RaycastHit ;

// Did we hit anything?
if (Physics.RaycastHit (transform.position, direction, hit, range)) {
    // Apply a force to the rigidbody we hit
    if (hit.rigidbody)
        hit.rigidbody.AddForceAtPosition(force * direction, hit.point);

    // Place the particle system for spawing out of place where we hit the surface!
    // And spawn a couple of particles
    if (hitParticles) {
        hitParticles.transform.position = hit.point;
        hitParticles.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);

but it said 'RayCastHit' is not a member of 'UnityEngine.Physics' would that script work fine if the RayCastHit was changed if not how could i edit it to make it fire at me snd do damage? thanks!

This gave me trouble the first time I dealt with Raycasts as well.

If you scroll down here: you will see an example of the proper way to use RaycastHit.

You basically create a RaycastHit (you would call it hit, in your code). This does not perform the raycast, but creates a container to store information about what the raycast hits. If you use `Physics.Raycast (transform.position, -Vector3.up, hit)` it will store the information in the RaycastHit specified by hit.

There might be other things wrong with your code but that is the thing that is immediately obvious to me.

Best way to see if it works is to test it. So to solve the problem you say, I would just change the Physics.RaycastHit to Physics.Raycast, for it seems like that is the problem. That should work, but as I said earlier,you should test it. Of course, I see no function or line of code that deals damage to a target. For that, you need to subtract the target's health (Which is defined in a player status script) when hit. Try looking at this question: Question

Good luck, I hope this helps.