To whomever it is reading this question, I really really need some help with a script that allows the player to cause some kind of damage to any enemy AI. Let me example I have managed to get a simple health script, and I’ve coded a basic AI system involving way points and triggers, at the moment the enemy AI can hurt/kill the player.
Now here’s the situation, I’m teaching a group of young kids ranging from 8 to 12 the basic unity with the intend of creation there own game by the end of there school year, so far they have gotten most of there level done, including a player that works and a simple enemy (zombie) to follow and attack them. The problem I’m having is that I’ve been able to script a simple code that pretty much just destorys the enemy once the player collider hits is (on trigger enter)
Know my problem is I went a simple script that pretty much states the enemy can take up to 3 hits before its death animation plays and it then dies. If anyone can help me, that would be great, thanks.
i suppose your player has a gun that shoots bullets
var hits : 0;
OnCollisionEnter(col : Collision){
if(col.gameObject.tag == "Bullet")
{
hits++;
}
}
function Update(){
if(hits = 3)
{
Destroy(gameObject);
}
}
Attach this to the enemy and set your bullet’s tag to “Bullet”. What this does it counts how many hits the enemy has - and each bullet that hits the enemy counts as a hit, so if the hits variable reaches 3 - then it destroys the enemy itself
Note
This is in JS
Now i dont really know if it would work flawlessly, cause im just 13, still learning how to script. But the concept is right, and it should work.
From 3D Platform (Lerpz) Tutorial. Isn’t activates by a “trigger”, but by using SendMessage("ApplyDamage", punchHitPoints). If the player press the punch button, this script search of all objects with “Enemy” tag. The ones that are in a short range and have the EnemyDamage script have this method call.
Use on enemy. EnemyDamage.js:
var hitPoints = 3;
var explosionPrefab : Transform;
var deadModelPrefab : Transform;
var healthPrefab : DroppableMover;
var fuelPrefab : DroppableMover;
var dropMin = 0;
var dropMax = 0;
// sound clips:
var struckSound : AudioClip;
private var dead = false;
function ApplyDamage (damage : int)
{
// we've been hit, so play the 'struck' sound. This should be a metallic 'clang'.
if (audio && struckSound)
audio.PlayOneShot(struckSound);
if (hitPoints <= 0)
return;
hitPoints -= damage;
if (!dead && hitPoints <= 0)
{
Die();
dead = true;
}
}
function Die ()
{
// Kill ourselves
Destroy(gameObject);
// Instantiate replacement dead character model
var deadModel = Instantiate(deadModelPrefab, transform.position, transform.rotation);
CopyTransformsRecurse(transform, deadModel);
// create an effect to let the player know he beat the enemy
var effect : Transform = Instantiate(explosionPrefab, transform.position, transform.rotation);
effect.parent = deadModel;
// fall away from the player, and spin like a top
var deadModelRigidbody = deadModel.rigidbody;
var relativePlayerPosition = transform.InverseTransformPoint(Camera.main.transform.position);
deadModelRigidbody.AddTorque(Vector3.up * 7);
if (relativePlayerPosition.z > 0)
deadModelRigidbody.AddForceAtPosition(-transform.forward * 2, transform.position + (transform.up * 5), ForceMode.Impulse);
else
deadModelRigidbody.AddForceAtPosition(transform.forward * 2, transform.position + (transform.up * 2), ForceMode.Impulse);
// drop a random number of pickups in a random fashion
var toDrop = Random.Range(dropMin, dropMax + 1); // how many shall we drop?
for (var i=0;i<toDrop;i++)
{
direction = Random.onUnitSphere; // pick a random direction to throw the pickup.
if(direction.y < 0)
direction.y = -direction.y; // make sure the pickup isn't thrown downwards
// initial position of the pickup
var dropPosition = transform.TransformPoint(Vector3.up * 1.5) + (direction / 2);
var dropped : DroppableMover;
// select a pickup type at random
if(Random.value > 0.5)
dropped = Instantiate(healthPrefab, dropPosition, Quaternion.identity);
else
dropped = Instantiate(fuelPrefab, dropPosition, Quaternion.identity);
// set the pickup in motion
dropped.Bounce(direction * 4 * (Random.value + 0.2));
}
}
/*
deadModel When we instantiate the death sequence prefab, we ensure all its child
elements are the same as those in the original robot, by copying all
the transforms over. Hence this function.
*/
static function CopyTransformsRecurse (src : Transform, dst : Transform)
{
dst.position = src.position;
dst.rotation = src.rotation;
for (var child : Transform in dst)
{
// Match the transform with the same name
var curSrc = src.Find(child.name);
if (curSrc)
CopyTransformsRecurse(curSrc, child);
}
}
@script AddComponentMenu("Third Person Enemies/Enemy Damage")
Use on player (Lerpz). ThirdPersonCharacterAttack.js:
var punchSpeed = 1;
var punchHitTime = 0.2;
var punchTime = 0.4;
var punchPosition = new Vector3 (0, 0, 0.8);
var punchRadius = 1.3;
var punchHitPoints = 1;
var punchSound : AudioClip;
private var busy = false;
function Start ()
{
animation["punch"].speed = punchSpeed;
}
function Update ()
{
var controller : ThirdPersonController = GetComponent(ThirdPersonController);
if(!busy && Input.GetButtonDown ("Fire1") && controller.IsGroundedWithTimeout() && !controller.IsMoving())
{
SendMessage ("DidPunch");
busy = true;
}
}
function DidPunch ()
{
animation.CrossFadeQueued("punch", 0.1, QueueMode.PlayNow);
yield WaitForSeconds(punchHitTime);
var pos = transform.TransformPoint(punchPosition);
var enemies : GameObject[] = GameObject.FindGameObjectsWithTag("Enemy");
for (var go : GameObject in enemies)
{
var enemy = go.GetComponent(EnemyDamage);
if (enemy == null)
continue;
if (Vector3.Distance(enemy.transform.position, pos) < punchRadius)
{
enemy.SendMessage("ApplyDamage", punchHitPoints);
// Play sound.
if (punchSound)
audio.PlayOneShot(punchSound);
}
}
yield WaitForSeconds(punchTime - punchHitTime);
busy = false;
}
function OnDrawGizmosSelected ()
{
Gizmos.color = Color.yellow;
Gizmos.DrawWireSphere (transform.TransformPoint(punchPosition), punchRadius);
}
@script RequireComponent(AudioSource)