When I create my enemy and I set them to spawn an ragdoll when they die, they sometimes spawn two or three ragdolls.
Here is my code that I use to the ragdoll spawning:
Enemy script:
var Distance;
var Target : Transform;
var lookAtDistance = 25.0;
var chaseRange = 15.0;
var attackRange = 1.5;
var moveSpeed = 5.0;
var Damping = 6.0;
var attackRepeatTime = 1;
var hurtSound : AudioClip;
var TheDammage = 40;
private var attackTime : float;
var controller : CharacterController;
var gravity : float = 20.0;
private var MoveDirection : Vector3 = Vector3.zero;
function Start ()
{
attackTime = Time.time;
}
function Update ()
{
if(RespawnMenuV2.playerIsDead == false)
{
Distance = Vector3.Distance(Target.position, transform.position);
if (Distance < lookAtDistance)
{
lookAt();
}
if (Distance > lookAtDistance)
{
}
if (Distance < attackRange)
{
attack();
}
else if (Distance < chaseRange)
{
chase ();
}
}
}
function lookAt ()
{
var rotation = Quaternion.LookRotation(Target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
}
function chase ()
{
moveDirection = transform.forward;
moveDirection *= moveSpeed;
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
function attack ()
{
if (Time.time > attackTime)
{
Target.SendMessage("ApplyDammage", TheDammage);
Debug.Log("The Enemy Has Attacked");
attackTime = Time.time + attackRepeatTime;
}
}
function ApplyDammage ()
{
chaseRange += 30;
moveSpeed += 2;
lookAtDistance += 40;
animation.Play("EnemyHurtAnime");
audio.play(hurtSound);
}
My Enemy Health script:
var ragdoll : GameObject;
var Health = 100;
function ApplyDammage (TheDammage : int)
{
Health -= TheDammage;
if(Health <= 0)
{
Dead();
}
}
function Dead()
{
SpawnRagdoll();
Destroy (gameObject);
}
function SpawnRagdoll ()
{
var clone = Instantiate(ragdoll, transform.position, transform.rotation);
Destroy (clone.gameObject, 5);
}
And lastly, bullet damage:
#pragma strict
var Dammage = 100;
function OnCollisionEnter (info : Collision)
{
info.transform.SendMessage("ApplyDammage", Dammage, SendMessageOptions.DontRequireReceiver);
Destroy (gameObject);
}
I also have to say thanks to Brackeys from youtube for the code.
http://www.youtube.com/user/Brackeys
↧