Problem with shooting monster

Hi, I have done a little script for a monster to let it shoot against my players. But I experiment a couple of problems.

First of all: Projectiles are thrown by the height of my monster feet, that's a problem. How I can modify this? Next: I don't know how to make this projectiles hurt my players, there's any tutorial? Or anyone can help me? I'm a little bit noobish with Unity scripting, I have uploaded a little video to show you my problems: http://www.youtube.com/watch?v=z7sCh5slI9c And here is the code:

function Shoot ()
{
    //Monster looks and moves briefly to player
    var angle : float;
    angle = 180.0;
    var time : float;
    time = 0.0;
    var direction : Vector3;
    while (angle > 5 || time < attackTurnTime)
    {
        animation.Play("attackrun");
        time += Time.deltaTime;
        angle = Mathf.Abs(RotateTowardsPosition(target.position, rotateSpeed));
        move = Mathf.Clamp01((90 - angle) / 90);

        // depending on the angle, start moving
        animation["attackrun"].weight = animation["attackrun"].speed = move;
        direction = transform.TransformDirection(Vector3.forward * attackSpeed * move);
        characterController.SimpleMove(direction);

        yield;
    }
    //Shoot against the objective
    var i : int;
    for(i=0; i<numberOfShoots;i++){
        animation.Play("fire");
            //random if (Input.GetButtonDown("Fire1")) {
            // Instantiate the projectile at the position and rotation of this transform
        var clone : Rigidbody;
        clone = Instantiate(projectile, transform.position, transform.rotation);

        // Give the cloned object an initial velocity along the current
        // object's Z axis
        clone.velocity = transform.TransformDirection (Vector3.forward * 50);

        // yield for one frame
        yield WaitForSeconds(reloadShoot);      
        }
}

Finally I have another problem, I don't know how to create a kind of array of players. I mean, to choose my monster target I use this function on the Initialitation:

if (!target)
    target = GameObject.FindWithTag("Player").transform;

But with this method I only select the first object with the tag Player. Anyone have an idea about this?

Ok, one step at a time. I'll try and answer as much questions before I'll have to go...

About the height of your projectiles - Instantiate is the method that creates them. Since you're using

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

They are created at the location "transform.position", which is your character's feet. What you need, is to replace transform.position (Which is a Vector3) with some other Vector3 where you want the projectiles to be instantiated.

For a no-fuss solution, just do:

clone = Instantiate(projectile, transform.position + Vector3.up * 2.0, transform.rotation);

This is a nasty solution, in my opinion, and it's easy to just provide the transform of a game object that is on the end of your gun's barrel. Create a new game object, move it to the end of the barrel, and parent it to the gun. This will make it stay with the gun during the animation. In your script, add:

public var tGunEnd: Transform;

After you save it and it compiles, select the game object which has the script on it. You can now drag your newly created game object (the one on the barrel end) to the new variable created in the inspector.

Then replace the earlier instantiate with:

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

To make those projectiles damage players, you'll need to find the collisions between the bullets and the players. This is usually done using MonoBehaviour.OnCollisionEnter or Collider.OnCollisionEnter. There are plenty of examples right here, in the forum or in the wiki. Read up on Unity's Physics and post questions if you have trouble understanding anything.

About your array issue, you can easily use GameObject.FindGameObjectsWithTag to find multiple objects. In fact, the second example on that page is exactly what you need.

Gotta go... Hope it helps.

I always use an empty as the spawn point for bullets. Just put an empty where the bullet should be created at, parent it to the monster, and give it the script to shoot. The bullet will come from wherever u put the empty.

As for taking damage, check out this tutorial:

http://www.youtube.com/watch?v=fxSM_INrmF8

It's melee combat, but u can use the same method and reduce health on collision with the bullet.