Using Raycast to shoot

I am very inexpereinced with Raycasts but i know they are one of the ways to make a gun 'shoot'. I want to do this for the simple reason that i wants to reduce clutter in the scene (i know how to destroy a object after it hits something or even over time, but it just seems messy to use GameObjects for bullets.) So i decided to try and make a raycast shooting script. Here is what i got so far:

var  par : ParticleEmitter;
var damage = 2;

function Update () {
var hit : RaycastHit;
    if (Input.GetMouseButtonDown(0)) {
        if (Physics.Raycast (transform.position,transform.forward,hit, 100)) {
            Instantiate(par, hit.transform.position, transform.rotation);
            if (hit.collider.gameObject.tag == "Enemy") {
                HealthAI.can_die(damage);
            }
        }
    }
}

Now the whole 'damage' and 'if (hit.collider.gameObject.tag == "Enemy")' thing is where i have the problem. I wish to make it so that when my ray hits something established as 'Enemy' it will access the function 'can_die' in the script 'HealthAI', here is that script.

static var aihitPoints = 10;

static function can_die ( damage ) {
    aihitPoints -= damage;
    if(aihitPoints < 1 ) {
    Destroy(gameObject, 0.0);
    }
}

function breakObj (damage) {
Destroy(gameObject, 0.0);
}

Now the whole problem with this is, A) It gives me a error saying the CapsuleCollider is not a memeber of other.SendMessage. B) gameObject is not a non static member. I have no idea how to fix this.

Also, when i instantiate the 'par' ParticleEmitter it always instanitates in the same direction, regardless of where it hits, any way to fix that? Quaternion.identity does not work, nor does transform.rotation. I want it so the sparks appear on the side the object was hit on.

Lastly, i want the ray to be only in the center of the screen, not the whole side of the camera (where i have the script attached) any way to make it do that? Mostly, just so when i point the reticule at something it hits just that.

The first thing you want to do is create a ray using Camera.ScreenPointToRay() instead of the camera object's transform.

Your problem with calling can_die() (besides the fact that you're not using proper camelCase), is that you are using a static function to reference nonstatic members. In general terms, this means that there can only be one instance of that function and one instance of the var aiHitPoints. So you could only have one AI object. What you should do instead is not use a static function, and instead SendMessage() or GetComponent.

For the particle emitter direction facing, use RaycastHit.normal. Quaternion.LookRotation() creates a rotation that looks along the normal. Position the emitter with RaycastHit.point.

Here's a sample of what you could do:

var  par : ParticleEmitter;
var damage = 2;

function Update () {

var hit : RaycastHit;
// Use Screen.height because many functions (like this one) start in the bottom left of the screen, while MousePosition starts in the top left
var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Input.MousePosition.x, Screen.height - Input.MousePosition.y,0));

    if (Input.GetMouseButtonDown(0)) {
        if (Physics.Raycast (ray, hit, 100, kDefaultRaycastLayers)) {

            Instantiate(par, hit.point, Quaternion.LookRotation(hit.normal));

            var otherObj : GameObject = hit.collider.gameObject;

            if (otherObj.tag == "Enemy") {
                otherObj.GetComponent(typeof(HealthAI)).CanDie(damage);
            }
        }
    }
}

The HealthAI script:

var aihitPoints : float = 10;

function CanDie ( damage : float) {
    aihitPoints -= damage;
    if(aihitPoints <= 0 ) {
      Destroy(gameObject);
    }
}

Of course, you're going to want to cache all these variables for performance reasons. You will also probably want to implement a LayerMask (replace kDefaultRaycastLayers). And unless you want your enemy to die at .99 hp, use <=0 instead of <1. You don't need to specify a time in the Destroy() function.

Hope that's all!