Code : Tout sélectionner
//ENG: Attach the script to the Enemy GameObject. this script make the enemy follow you, and, hurts your player if is in a certain distance
//ESP: Adjunta el Script a un objeto enemigo. Este script hace que el enemigo persiga al jugador y, si esta a cierta distancia, le hace daño.
private var ismoving=false; //ENG: is moving the Enemy?
//ESP: Se esta moviendo el enemigo?
private var target : GameObject; //ENG: Enemy´s Target
//ESP: El objetivo del enemigo
var enemyvelocity:int=5; //ENG: Enemy´s Velocity
//ESP: Velocidad del enemigo
private var attaking:float; //ENG: attack time rate
//ESP: ratio de ataque del enemigo
function Start(){
//ENG: set the target and starts to moving at start
//ESP: especifica el objetivo y empieza a moverse
target = GameObject.FindWithTag("Player");
ismoving = true;
}
function Update () {
//ENG: if not moving, wait one second, else, if the enemy have a target, chase that target (player)
//ESP: si no se mueve, espera un segundo, si no, si el enemigo tiene objetivo, ir a por él(jugador)
if(!ismoving)
{
attaking += Time.deltaTime;
if(attaking > 1)
{
ismoving=true;
attaking = 0;
}
}
else
{
if(target)
ChasePlayer(target);
}
}
function ChasePlayer(target:GameObject)
{
//ENG: look at target
//ESP: mirar al objetivo
var direction = target.transform.position - transform.position;
direction.y=0;
var rotation = direction;
//ENG: rotate smoothly to the target´s direction
//ESP: rotar suavemente hacia la dirección del jugador
transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation(direction), 3 * Time.deltaTime);
transform.eulerAngles = Vector3(0, transform.eulerAngles.y, 0);
//ENG: stablish the orientation
//ESP: establecer la orientación
var forward = transform.TransformDirection(Vector3.forward);
//ENG: Stablish the velocity variance depend on the target´s direction
//ESP: establecer la varianza de la velocidad dependiendo de la dirección del objetivo
var speedModifier = Vector3.Dot(forward, direction.normalized);
speedModifier = Mathf.Clamp01(speedModifier);
//ENG: if target reaches a certain distance between him and the player, hurts the player and stop moving, else, it continues moving
//ESP: si el objetivo alcanza cierta distancia entre el y el jugador, hiere al jugador y para de moverse. de lo contrario, continua moviendose
if (Vector3.Distance(transform.position, target.transform.position) < 1.5)
{
ismoving=false;
target.SendMessageUpwards("ApplyDamage",5,SendMessageOptions.DontRequireReceiver);
}
else
{
direction = forward * enemyvelocity * speedModifier;
if(GetComponent (CharacterController))
GetComponent (CharacterController).SimpleMove(direction);
else
{
rigidbody.velocity=direction;
rigidbody.velocity.y=-1;
}
}
yield;
}