• 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
Question by Jazzds · Jul 02, 2015 at 10:31 AM · movementaienemymerge

Merging my enemy AI and target script together smart?

So I have been having troubles with disabling my movement script for when the enemy dies off. I thought maybe if I just merge the scripts together, I would be easily able to access the movement functions and disable it from the inside. But then I didn't know if this was a smart move since they are two separate functions to how the enemy works. I'll post both codes down below and you can tell me what you think. Just trying to disable the script has not been working with the getComponent().enabled thing.

 //Enemy Health Script
 #pragma strict
 
 var curHealth : float = 30f;
 var healthBar: UnityEngine.UI.Slider;
 var isAttacked: boolean;
 var isDead: boolean;
 var animator : Animator;
 //var maxHealth : float = 100f;
 
 function Start () {
     isAttacked = false;
     isDead = false;
 //    healthBar = UI.Slider.FindObjectOfType(UI.Slider);
     animator = GetComponent(Animator);
 
 }
 
 function Update () {
 
 //    healthtext.text = curHealth + " / " + maxHealth;
 //    healthBar.value = curHealth;
     
     if(curHealth <= 0 ) {
         curHealth = 0;
     }
 
     if(curHealth >= 30) {
         curHealth = 30;
     }
 
     if (isAttacked == true) {
         animator.SetBool("isDamaged", true);
 
     }
     else { animator.SetBool("isDamaged", false);}
     
     if(isDead == true){
         animator.SetBool("isDead", true);
         }
 
 }
       function OnTriggerExit(collider:Collider) {
       
           if (collider.tag == "Player" || collider.tag == "Weapon"){
               if(curHealth == 0) { 
                   isDead = true;
                   
                   }
                   else {
                        curHealth -= 10;
                        isAttacked = true;
                    }    
     }
 }
 
 //Enemy AI Script 
 #pragma strict
  
  var target : Transform; //the enemy's target
 var targetHealth : int;
  var moveSpeed = 3; //move speed
  var rotationSpeed = 3; //speed of turning
  var range : float=15f;
  var range2 : float=15f;
  var stop : float=1;
  var myTransform : Transform; //current transform data of this enemy
  var anim : Animator;
  function Awake()
  {
      myTransform = transform; //cache transform data for easy access/preformance
      
  }
   
  function Start()
  {
       target = GameObject.FindWithTag("Player").transform; //target the player
       anim = GetComponent(Animator);
       if (!target == null) {
           anim.SetFloat("foundTarget", 1.0);
           }
  }
   
  function Update () {
      //rotate to look at the player
      var distance = Vector3.Distance(myTransform.position, target.position);
      if (distance >= range2) {
          anim.SetFloat("foundTarget", 1.0);
      }
      if (distance<=range2 &&  distance>=range){
      myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
      Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
      }
    
  
      else if(distance<=range && distance>stop){
  
      //move towards the player
      anim.SetFloat("targetDistance", distance);
      myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
      Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
      myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
      }
      else if (distance<=stop) {
      myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
      Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
      anim.SetFloat("targetDistance", distance);
      anim.SetBool("canAttack", true);
      }
      else {
          anim.SetFloat("targetDistance", distance); 
          anim.SetBool("canAttack", false); }
          
      
  }
  
 
Comment

People who like this

0 Show 0
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

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by LaneFox · Jul 02, 2015 at 11:13 AM

There isn't any reason you could not merge the two scripts, but there may be incentive to keep them separate.. Typically when scripts start getting more complicated you can start to make Update much more high level.

You could do something like.. (C#)

         void Update()
         {
             DoHealthProcess(); // here's your health script
             DoMovement(); // here's your movement script
             DoOtherStuff(); // heres some other magic
         }

Then just put your two separate scripts into the other methods and let a single update invoke them.

However it could be to your advantage in the long term to keep Health separate so it is easier to access generically. For instance when you want to start damaging things you'll have a specific script you can target which will deal with processing damage and collisions that will always have the same class name and method names. If you put Health inside a different script, you may have a harder time accessing the Health between different enemies/classes.

Comment
Jazzds

People who like this

1 Show 2 · Share
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 Jazzds · Jul 11, 2015 at 07:37 PM 0
Share

Thank you for your advice! I forget about just making stuff in its own methods!

avatar image starikcetin · Jul 11, 2015 at 07:43 PM 0
Share

Good point on script management, but this single-update issue isn't required since update is called once per frame. You can just use update in every script individually.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Enemy controller.move script slows down and speeds up. How to get it consistent? 0 Answers

Enemy following Player on uneven surface 1 Answer

AI controller script, Enemy movement issues 0 Answers

Unity2D, How do i check which way a sprite is facing? 1 Answer

How to make basic AI in a 2d game? 4 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