- Home /
Question by
Shikomizue · Jun 10, 2015 at 10:22 PM ·
collision detectioncharacter controllerdamageenemy ai
Enemy Ai Damage gets done multiple times
Hi there, i have a problem with my Enemy AI, and that is that my enemy should perform a jumpcharge attack which should do 2 damage to my 8 maxHealth Player. Problem is if my enemy jumps my direction i do a get the distance to the enemy to see if it hits, and this works, but the damage gets substracted a lot more, that i end up dead with like -10 health from one jump. Furthermore i want the enemy to stop its movements for two seconds after it jumps, but it just continues moving in the direction which it jumped.
Here is my script so far, help would be appreciated:
var Distance;
var Target : Transform;
var lookAtDistance = 15.0 ;
var chaseRange = 15.0 ;
var moveSpeed = 5.0;
var Damping = 6.0;
var chargeRange = 1.5 ;
var chargeHitDistance = 1;
var damage = 2;
var controller : CharacterController ;
var gravity : float = 100.0 ;
private var moveDirection : Vector3 = Vector3.zero;
var damageOnce = false;
var animator : Animator ;
var chargeReady = true;
function Start ()
{
attackTime = Time.time;
Target = GameObject.Find("Player").transform;
animator = gameObject.GetComponent(Animator);
}
function Update ()
{
if(RespawnMenue.playerIsDead == false)
{
Distance = Vector3.Distance(Target.position, transform.position);
if(Distance < lookAtDistance && Distance > chargeRange)
{
lookAt();
}
if(Distance > lookAtDistance)
{
GetComponent.<Renderer>().material.color = Color.green;
}
if(Distance < chargeRange && chargeReady == true)
{
charge();
}
else if(Distance < chaseRange )
{
animator.SetBool("Run" , true);
chase();
}
}
}
function lookAt()
{
GetComponent.<Renderer>().material.color = Color.yellow;
var rotation = Quaternion.LookRotation(Target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation , Time.deltaTime * Damping);
}
function chase()
{
GetComponent.<Renderer>().material.color = Color.red;
moveDirection = transform.forward;
moveDirection *= moveSpeed;
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
function charge ()
{
chargeReady = false;
animator.SetBool("Charge", true);
yield;
moveDirection = transform.forward;
moveDirection *= moveSpeed;
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
animator.SetBool("Charge", false);
animator.SetBool("Run" , false);
damageOnce = false;
Pause();
}
function ApplyDamage ()
{
animator.SetBool("Stagger" , true);
yield;
animator.SetBool("Stagger" , false);
}
function Pause()
{
if (Distance < chargeHitDistance)
{
if (damageOnce == false)
{
damageOnce = true;
Target.SendMessage("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
}
}
chargeReady = true;
}
Comment