Hey guys. In a combat system for a game I’m making, one script which contains the damage scripts sends a message to a health script, telling it to take the defense damage variable sent off of the defense total in the health script. If the defense is equal to or less than zero, a message is sent back to the damage script, which is supposed to, the next time an attack move is executed, send a message back to the health script which takes health damage off of the health total. See:
#pragma strict
var stabDamage : int = 500;
var stabDefenceDamage : int = 5;
var quickSwingDamage : int= 200;
var quickSwingDefenceDamage : int = 10;
var heavySwingDamage : int = 300;
var heavySwingDefenceDamage : int = 25;
var distance : float;
var maxDistance : float = 2.5;
var gladius : Transform;
function Update () {
if(Input.GetMouseButtonDown(0)){
gladius.animation.Play("quickSwing");
var hit1 : UnityEngine.RaycastHit;
if(Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), hit1)){
distance = hit1.distance;
if( distance < maxDistance){
hit1.transform.SendMessage("AlterDefence", quickSwingDefenceDamage, SendMessageOptions.DontRequireReceiver);
}
}
}
if(Input.GetMouseButtonDown(1)){
gladius.animation.Play("heavySwing");
var hit2 : UnityEngine.RaycastHit;
if(Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), hit2)){
distance = hit2.distance;
if( distance < maxDistance){
hit2.transform.SendMessage("ApplyDamage", heavySwingDamage, SendMessageOptions.DontRequireReceiver);
}
}
}
if(Input.GetButtonDown("stab")){
gladius.animation.Play("stab");
var hit3 : UnityEngine.RaycastHit;
if(Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), hit3)){
distance = hit3.distance;
if( distance < maxDistance){
hit3.transform.SendMessage("ApplyDamage", stabDamage, SendMessageOptions.DontRequireReceiver);
}
}
}
if(gladius.animation.isPlaying == false){
gladius.animation.CrossFade("Idle");
}
}
function QuickSwingDamage(){
var hit1 : UnityEngine.RaycastHit;
if(Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), hit1)){
distance = hit1.distance;
if( distance < maxDistance){
hit1.transform.SendMessage("ApplyDamage", quickSwingDamage, SendMessageOptions.DontRequireReceiver);
}
}
}
and:
var health = 500;
var defence = 100.00;
var defenceModifier : int;
var evadeChance : int;
function Update(){
if(health <= 0){
Dead();
}
if(defence <= 0){
SendMessage("QuickSwingDamage", SendMessageOptions.DontRequireReceiver);
}
}
function ApplyDamage(damage : int){
health -= damage;
}
function AlterDefence(defenceDamage : int){
defence -= defenceDamage;
}
function Dead(){
Destroy (gameObject);
Currently, nothing happens, in fact defense goes in to negative values instead of staying at 0 and enacting health damage to the health script values. I’m pretty new at using unity so it’s probably a little thing that I haven’t noticed. Thanks for the helps guys!