- Home /
Problem with 2 scripts communicating
I have a script called "Attack.js" attached to my player character. I also have another script called "MinionAttack.js" attached to an enemy. What I need is for the enemy's health to decrease by a certain amount after each attack animation the player executes. (I'm using "GetComponent" then subtracting from my "enemyHealth" variable.) This works fine, but my problem is that I need for the MinionAttack script to check not only if the attack animations are playing, but also check to make sure the player is within a certain distance from the enemy. I can damage the enemy just fine without the distance check, but once I add that part in, start the game and attack the enemy up close, it's health doesn't decrease at all. I'm pretty confused, because everything looks like it should be working... Can someone please point out what I'm doing wrong?
Attack.js
var MinionAttack;
var distance;
var enemy1 : Transform;
var combos : ComboSequence[] = new ComboSequence[3]; //Combo class. Fill this out in Inspector.
var comboMaxTime : float = 1.0; //How long a timer for combo lasts in seconds. Fill this out in Inspector.
var comboSpanTime : float = .25; //The span of when a combo can start. Fill this out in Inspector.
var comboMidPoint : float = .5; //The position within the maxtime of a combo the span is active. Fill this out in Inspector.
private var currentCombo : int = 0;
private var comboTimeout : float = .0;
animation["walk"].layer = 1;
animation["run"].layer = 1;
animation["stand2"].layer = 1;
animation["attack1"].layer = 4;
animation["attack2"].layer = 5;
animation["attack3"].layer = 6;
distance = Vector3.Distance(enemy1.position, transform.position);
function Start (){
}
function Update () {
if (Input.GetButtonDown("Attack")) Attack();
if (comboTimeout > 0) DecreaseTime();
if (Mathf.Abs(Input.GetAxis("Forward")) > 0.1)
animation.CrossFade("walk");
else
animation.CrossFade("stand2");
if(Input.GetAxis("Forward") && (Input.GetAxis("Run"))
)animation.CrossFade("run");
}
function Attack () {
var damageRange : float = 2.0;
var enemyScript = MinionAttack;
MinionAttack = GameObject.Find("CaveWorm_anim").GetComponent("MinionAttack");
ComboTime(); //Do all the logic for timing before animation etc.
animation.CrossFade(combos[currentCombo].comboAnimation); //Play the currentCombo combos' animation
audio.clip = combos[currentCombo].comboSound; //Set the audio clip to the currentCombo combos' audio
audio.Play(); //Finally play the audio
//Add logic for hitting enemies etc here!
if(distance < damageRange && animation["attack1"].enabled)
{
enemyScript.enemyHealth -= 10;
}
if(distance < damageRange && animation["attack2"].enabled)
{
enemyScript.enemyHealth -= 20;
}
if(distance < damageRange && animation["attack3"].enabled)
{
enemyScript.enemyHealth -= 40;
}
}
function ComboTime () {
if (currentCombo<combos.Length-1 &&
comboTimeout>0 &&
comboTimeout>comboMidPoint-comboSpanTime &&
comboTimeout<comboMidPoint+comboSpanTime ||
currentCombo==-1) {
currentCombo++;
} else {
currentCombo = -1;
}
comboTimeout = comboMaxTime;
}
function DecreaseTime () {
comboTimeout-=1*Time.deltaTime;
}
class ComboSequence {
var comboName : String; //A name for current attack (might be of use later for tutorial or something else)
var comboAnimation : String; //The animation name for current attack
var comboSound : AudioClip; //The audio for current attack
}
MinionAttack.js
var attackDelay = 1.0;
var distance;
var target : Transform;
var lookAtDistance = 15.0;
var attackRange = 10.0;
var moveSpeed = 5.0;
var damping = 6.0;
var hitRange = 10.0;
var idleRange = 10.0;
public var smash : AudioClip;
private var isItAttacking = false;
var audio1: AudioSource;
var audio2: AudioSource;
var audio3: AudioSource;
var distanceFromTarget : float = 4.0;
var hasSeenPlayer = false;
var musicDistance = 10.0;
var soundPlayed = false;
public var enemyHealth = 130;
var enemy : GameObject;
var dieDelay = 0.20;
private var _target : Transform;
function Update ()
{
var aSources = GetComponents(AudioSource);
audio1 = aSources[0];
audio2 = aSources[1];
audio3 = aSources[2];
distance = Vector3.Distance(target.position, transform.position);
if(distance < lookAtDistance)
{
isItAttacking = false;
lookAt ();
}
if(distance > lookAtDistance && hasSeenPlayer == true)
{
lookAt ();
attack ();
}
if(distance < attackRange)
{
attack ();
animation.CrossFade("walk");
hasSeenPlayer = true;
}
if(distance > attackRange && hasSeenPlayer == true)
{
attack ();
}
if(animation["walk"].enabled && animation["walk"].time == 0)
{
audio3.Play();
musicDistance = 100.0;
}
if(isItAttacking)
{
}
if(distance < hitRange)
{
animation.CrossFade("attack");
}
if(animation["attack"].enabled && animation["attack"].time == 0)
{
audio2.Play();
}
if(distance > idleRange && hasSeenPlayer == false)
{
animation.CrossFade("idle");
}
if(distance < distanceFromTarget)
{
moveSpeed = 0;
}
if(distance > distanceFromTarget)
{
moveSpeed = 7.38;
}
if(distance < musicDistance && soundPlayed == false)
{
audio1.Play();
soundPlayed = true;
}
if(enemyHealth <= 0)
{
Die();
}
}
function lookAt ()
{
var rotation = Quaternion.LookRotation(target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
}
function attack ()
{
isItAttacking = true;
transform.Translate(Vector3.forward * moveSpeed *Time.deltaTime);
animation.CrossFade("walk");
}
function Die ()
{
animation.CrossFade("dead");
yield WaitForSeconds (dieDelay);
Destroy (enemy);
}
These are both scripts that others have helped me out with, so some parts I'm not sure I understand. That's why I'm asking here. Thanks in advance for any help. :)
First and foremost, you should be using the start function for your initialization....
You set your damage range to two, that seems like a really small distance from the player. Have you tried increasing that value?
Also, you use the Transform enemy1 in your distance calculation, but I can't find where you set the transform....
Your answer
Follow this Question
Related Questions
Enemy variable remains null 1 Answer
Enemy Health Problems 1 Answer
how do i make an Enemy take damage from prefab bullet clone? 1 Answer
Calling a variable from one script to another 1 Answer
AddComponent with parameter variable 2 Answers