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?