Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by SirBedlam · Jul 18, 2012 at 01:39 AM · variableenemygetcomponenthealth

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. :)

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image NathanJaremko · Jul 18, 2012 at 02:19 AM 0
Share

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....

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges