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 Leo0813 · Aug 31, 2021 at 10:14 AM · enemyenemy ai

how to make enemy go around the other enemy to attack player,not just move,then keep push enemy.

 public class blueMonster : MonoBehaviour
 {
     [SerializeField] private float detect, speed, attackR, setTime; 
     private Vector3 enemy;
     private Vector2 sta, end;
     private Animator animator;
     private RaycastHit2D hit;
     private float mv, dis, noMove; 
     public MonsterState currentState;
     private List<GameObject> k;
 
     void Start()
     {
         currentState = MonsterState.Idle;
         k = new List<GameObject>();
         animator = GetComponent<Animator>();
         mv = -1f;
         speed = 1.5f;
         attackR = 1.5f;
         setTime = 1f;
         detect = transform.Find("detectRange").gameObject.GetComponent<CircleCollider2D>().radius;
         dis = detect;
         animator.SetFloat("moveX", mv);
         StartCoroutine(waitIdle(setTime));
     }
 
     private IEnumerator waitIdle(float t)
     {
         yield return new WaitForSeconds(t);
         currentState = MonsterState.walk;
         animator.SetBool("moving", true);
     }
 
     void Update()
     {
         monsterBehavior();
     }
 
 
     void monsterBehavior()
     {
         if (dis <= attackR && currentState != MonsterState.attack)
         {
             animator.SetBool("moving", false);
             StartCoroutine(AttackCo());
         }
         else if (currentState == MonsterState.track)
         {
             Vector2 direction = enemy - transform.position; 
             transform.position = Vector3.MoveTowards(transform.position, enemy, Time.deltaTime * speed); //移動
             animator.SetFloat("moveX", direction.x);
             animator.SetFloat("moveY", direction.y);
             animator.SetBool("moving", true);
         }
         else if (currentState == MonsterState.walk)
         {
             transform.position += new Vector3(-(speed), 0, 0) * Time.deltaTime; //往前走
             animator.SetFloat("moveX", mv);
             animator.SetFloat("moveY", 0);
             animator.SetBool("moving", true);
         }
     }
 
     private IEnumerator AttackCo()
     {
         animator.SetBool("attacking", true);
         currentState = MonsterState.attack;
         yield return null;
         animator.SetBool("attacking", false);
         yield return new WaitForSeconds(1f);
         currentState = MonsterState.track;
     }
 
     private void OnTriggerStay2D(Collider2D other)
     {
         if (other.CompareTag("red"))
         {
             if (k.Count == 0)
             {
                 k.Add(other.gameObject);
             }
             else if (other.gameObject == k[0])
             {
                 currentState = MonsterState.track;
                 enemy = other.transform.position;
                 dis = Vector3.Distance(transform.position, enemy); //兩座標之間的距離長度
             }
         }
     }
 
     private void OnTriggerExit2D(Collider2D other)
     {
         if (other.CompareTag("red"))
         {
             if (k.Count > 0 && other.gameObject == k[0])
             {
                 k.Remove(other.gameObject);
                 dis = detect;
                 currentState = MonsterState.walk;
             }
         }
     }
 }

alt text

Tell me good solutation,I really need to help.PLEASE~~~!!!

未命名.png (22.7 kB)
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by yuvaansh · Aug 31, 2021 at 12:22 PM

@awdrg052
You can use Raycast to determine if an enemy is standing in Front of the other enemy and if it is then move to either left or right & you should also add code in which after the enemy has moved right/left then it should again do an Raycast to see if any other enemy is standing in front of the current enemy. I hope that made sense & answered your question
;-D

Comment
Add comment · Show 9 · 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 Leo0813 · Aug 31, 2021 at 03:06 PM 0
Share

@yuvaansh

  1. how do I use the Raycast ? I had search this but I still no how to use.

  2. how do I know the other Enemy move to left or to right ,trigger or something else?

avatar image yuvaansh Leo0813 · Sep 01, 2021 at 04:16 AM 0
Share

@awdrg052
Sorry for the late reply. But you should use Raycast like this.
RaycastHit object;
if (Physics.Raycast(enemy.transform.position, enemy.transform.forward, out object, range) && object.transform.tag == "Enemy") {
//Move left/right code
}

Also a couple more things that I think you have this script on the Enemy prefab (if you have prefab). Second thing is that you should add a tag to the Enemy prefab named whatever you want for reference I have written Enemy in the script & you can also use "this.gameobject .transform" in the script but just make things short i just wrote enemy. Last that this should be in the Enemy script. I hope this helped you :D

avatar image Leo0813 yuvaansh · Sep 01, 2021 at 08:29 AM 0
Share

@yuvaansh

I use your method but still not working.I don't understand why.

 public class NewBehaviourScript : MonoBehaviour
 {
     public float speed, max = 10;
     RaycastHit k;
     // Start is called before the first frame update
     void Start()
     {
 
     }
 
     // Update is called once per frame
     void Update()
     {
         Debug.DrawLine(transform.position, transform.forward, Color.black);
         if (Physics.Raycast(transform.position, transform.forward, out k, max) && k.transform.tag=="enemy")
         {
             **Debug.Log("test");**
         }
         transform.position += new Vector3(-(speed), 0, 0) * Time.deltaTime;
     }
 }
 
 

alt text

未命名.png (134.4 kB)
Show more comments
avatar image
0

Answer by Riiich · Aug 31, 2021 at 06:14 PM

You'll want to use some kind of pathfinding. My recommendation is the free A* Pathfinding project for Unity: https://arongranberg.com/astar/

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

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

128 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Enemies do damage even after dying 1 Answer

Enemy walking off platform in 2D mode 1 Answer

How do I make a 2d enemy change direction upon hitting a wall? 1 Answer

How can i get a specific XYZ position from character's animation that still move? 0 Answers

How to implement proper enemy follow and shoot 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