Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Will-Urquhart · May 18, 2017 at 12:37 AM · collisioncolliderstriggersfollowenemy ai

How do I create AI with a good side and evil side?

Basically the script I have takes uses a trigger. when something with the tag player, GoodGuy, or Enemy collides with the trigger, the AI with the collider should follow the collidee. The problem is, after one object has collided with the trigger it for some reason wont work. Here is my script...

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class stalk : MonoBehaviour { public GameObject TheCollider; public GameObject DefaultFollow; public bool CanAttack; public bool Attack1; public bool Attack2; public bool CanFollow = true; public int teamnum; public float distance; public int colliderteamnum; public bool following; private Team teamScript; private AI aiScript; public Animator anim; void Start () { teamScript = this.gameObject.GetComponent (); aiScript = this.gameObject.GetComponent (); teamnum = teamScript.team; anim = GetComponent (); }

 void Update () {

     distance = Vector3.Distance (this.transform.position, TheCollider.transform.position);

     // FOLLOWING MECHANICS




     if (following) {
         //follows the collided object
         aiScript.agent.SetDestination (TheCollider.transform.position);
         Vector3 eulerBefore = this.transform.eulerAngles; // or localEulerAngles, dependent on your needs
         this.transform.LookAt (TheCollider.transform);
         eulerBefore.y = this.transform.eulerAngles.y;
         this.transform.eulerAngles = eulerBefore;
         CanFollow = false;
         aiScript.agent.speed = 0.4f;
         anim.SetBool ("Walk", false);
         anim.SetBool ("Run", true);

         if (CanAttack) {

             anim.SetBool ("Run", false);
             anim.SetBool ("AttackA", true);
             aiScript.agent.speed = 0f;
             Invoke ("A1", 1f);

             Invoke ("A2", 3.3f);
         }

         if (Attack1) {
             anim.SetBool ("AttackB", true);
             anim.SetBool ("AttackA", false);
             Invoke ("A2", 1.5f);
         }
         if (Attack2) {
             anim.SetBool ("AttackC", true);
             anim.SetBool ("AttackB", false);
             Invoke ("A3", 0.9f);
         }



         if (CanAttack == false) {
         anim.SetBool ("Walk", false);
         anim.SetBool ("Run", true);
         anim.SetBool ("AttackA", false);
         anim.SetBool ("AttackB", false);
         anim.SetBool ("AttackC", false);
             Attack1 = false;
             Attack2 = false;


         }


         if (TheCollider.GetComponent<Life> ().dead == true) {

             following = false;
         }

     }

         if (following == false) {
             anim.SetBool ("Walk", true);
             anim.SetBool ("Run", false);
         anim.SetBool ("AttackA", false);
         anim.SetBool ("AttackB", false);
         anim.SetBool ("AttackC", false);
             aiScript.agent.speed = 0.1f;
             CanFollow = true;
             Attack1 = false;
             Attack2 = false;
             CanAttack = false;
         }



         //ATTACKING MECHANICS
     if (distance < 3.3f) {
             CanAttack = true;
         }

         if (distance > 3.4f) {
             CanAttack = false;
         }



     

 }

 public void OnTriggerStay(Collider other){

     //tests to see if object is a player
     if (CanFollow == true) {
         if (other.tag == "Player" || other.tag == "GoodGuy" || other.tag == "Enemy") {



                 // sets the collider game object to the object that has collided with the trigger

                 if (colliderteamnum > teamnum || colliderteamnum < teamnum) {
                     following = true;
         
                     TheCollider = other.gameObject;

                 }


             if (colliderteamnum == teamnum) {
                     following = false;
                     TheCollider = DefaultFollow;
                 }
                 // finds the TeamScript on the collider and turns colliderteamnum to that variable
                 colliderteamnum = TheCollider.gameObject.GetComponent<Team> ().team;
             } 







 }


 }


 public void OnTriggerExit(Collider other){
     if (other.tag == "Player" || other.tag == "GoodGuy" || other.tag == "Enemy") {
         if (colliderteamnum > teamnum || colliderteamnum < teamnum) {
             following = false;
             TheCollider = DefaultFollow;
         }
         if (colliderteamnum == teamnum) {
             following = false;
             TheCollider = DefaultFollow;
         }
     }

 }

 public void A1(){
     Attack1 = true;
 }

 public void A2(){
     Attack2 = true;
     Attack1 = false;
 }
 public void A3(){
     Attack2 = false;
     Attack1 = false;
 }




}

The Collider does right to take into account the team numbers and the tags and the AI follows and attacks and everything but when another object collides with it, the object stops following all objects all together.. If anyone has a way they know to fix this it would be greatly appreciated, Thank you.

Comment
Add comment
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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

changing gravity OnTriggerEnter 1 Answer

Trying to make object turn red OnTriggerEnter 0 Answers

Curved 3d Collider 1 Answer

How to single out specific colliders from another object 1 Answer

Why Trigger colliders costs more than non trigger ones? 0 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