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 Skobizzle · Sep 03, 2020 at 08:36 PM · movementdistancecircularobstacle avoidancecosine

Enemy avoid other enemies while pursuing player

So I'm trying to come up with a relatively simple way for enemies to avoid each other within a radius while continuing to pursue the player, and I'm running into an issue where they WILL avoid each other with the method im using, but the issue is that they snap immediately into a position away from the other enemy.
The goal here is to, when the enemy is NOT close enough to attack, and the enemy's distance to the other enemy is within "distanceToOther", to rotate circularly over time towards the direction of the player. It's moving in the correct direction, just not over a period of time smoothly, just snapping directly to it.

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using UnityEngine;
 
 public class Enemy : MonoBehaviour
 {
     public Transform player;
     [Range(0, 3f)]
     public float moveSpeed = 2f;
     [Range(0, 20)]
     public float damage;
     [Range(0, 20)]
     public float attackRadius;

     private List<GameObject> otherEnemies = new List<GameObject>();
     [Range(0f, 3f)]
     public float distanceToOther;
     Vector3 otherDirection;
     private float pivotAngle;
 
     private Rigidbody2D rb;
     Vector3 direction;
 
     [Range(0, 5)]
     public float RotateSpeed = 2f;
     [Range(0, 5)]
     public float Radius = 1f;
 
     private Vector3 pivot;
     private float rotateAngle;
     Vector3 offset;
 
     private void Start()
     {
         rb = GetComponent<Rigidbody2D>();
         if (GameObject.FindGameObjectsWithTag("Enemy") != null)
         {
             foreach (GameObject enemy in GameObject.FindGameObjectsWithTag("Enemy"))
             {
                 if (enemy != this.gameObject)
                 {
                     otherEnemies.Add(enemy);
                 }
             }
         }
         if (GameObject.FindWithTag("Player") != null)
         {
             player = GameObject.FindWithTag("Player").transform;
         }
         
     }
 
     public void Update()
     {
         rotateAngle += RotateSpeed * Time.deltaTime;
         offset = new Vector3(Mathf.Sin(rotateAngle), Mathf.Cos(rotateAngle)) * distanceToOther;
 
         if (player)
         {
             FindPlayerLocation();
         }
         if (otherEnemies.Count > 0)
         {
             FindOtherEnemyLocations();
         }
     }
 
     private void FindPlayerLocation()
     {
         direction = player.position - transform.position;
     }
 
     private void FindOtherEnemyLocations()
     {
         foreach (GameObject other in otherEnemies)
         {
             if (other)
             {
                 otherDirection = gameObject.transform.position - other.transform.position;
             }
         }
     }
 
     public void FixedUpdate()
     {
         if (player)
         {
             if (Vector3.Distance(player.position, transform.position) > attackRadius)
             {
                 rb.MovePosition(transform.position + direction.normalized * moveSpeed * Time.fixedDeltaTime * 100);
 
                 if (otherEnemies.Count > 0)
                 {
                     foreach (GameObject other in otherEnemies)
                     {
                         if (other)
                         {
                             if (Vector3.Distance(transform.position, other.transform.position) <= distanceToOther)
                             {
                                 pivot = other.transform.position;
                                 transform.position = pivot + offset;
                             }
                         }
                     }
                 }
 
             }
             else
             {
                 //check if still cooling down
                 if (attackCooldown > 0)
                 {
                     //decrement if still cooling down
                     attackCooldown--;
                 }
                 //check attack cooldown
                 if (attackCooldown == 0)
                 {
                     AttackPlayer();
                     //reset the attack cooldown
                     attackCooldown = baseAttackCooldown;
                 }
             }
         }
     }

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Rienhl · Sep 04, 2020 at 07:40 AM

There are some conceptual errors in the way you are using FixedUpdate and mixing up Rigidbody-based movement with Transform-based movement.

You are updating the enemies position with MovePosition, but then you also change the transform directly, so this kind of mixes up and overrides the MovePosition from the Rigidbody.

When you use Physics to move a player, you are "delegating" control of the Transform to the Rigidbody. If you then modify the transform values by code, you can get unwanted results.

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

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

Stopping navmesh agent next to the enemy 0 Answers

Rigidbody slows down before touching its target 1 Answer

Rotate object to follow circular movement 2D 0 Answers

focusing on a unit during unit detection in an RTS 1 Answer

Move player in a circular manner 1 Answer


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