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 masaegg · Jan 11, 2018 at 11:42 AM · unity 5enemyfollow player

Unity3D : Enemies following the player keep moving through each other

Hi, I'm trying to make enemies follow the player, but also for them to collide with each other and other objects in my game. The "following" part works (I found some help for that here already), but as soon as I have multiple enemies, doesn't matter where they're placed, after some time chasing me they kind of all form into one, they overlap each other. This is the code for following the player : [CODE]using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;

public class follow : MonoBehaviour {

 public Transform Player;

 public float MoveSpeed = 6f;
 float MaxDist = 30f;
 float MinDist = 5f;

 void Update()
 {
     transform.LookAt(Player);

     if (Vector3.Distance(transform.position, Player.position) >= MinDist)
     {

         transform.position += transform.forward * MoveSpeed * Time.deltaTime;

     }
 }

}[/CODE]

I'd really appreciate some help, I've been at this for quite some time.

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 BastianUrbach · Jan 11, 2018 at 12:01 PM

If you want enemies to be affected by Physics, you have to work with Unity's Rigidbody Component instead of modifying their position directly. If you set the position directly, the Rigidbody doesn't know your "intention", you might just want the object to "teleport" to its new position, rather than moving it smoothly and with respect to physics. To use Rigidbodies and allow collisions, try the following:

  • Attach a Collider component (e.g. "Sphere Collider") to your player and to the enemies if you haven't done so yet.

  • Attach a Rigidbody component to the enemies

  • Change

    transform.position += transform.forward * MoveSpeed * Time.deltaTime;
    to
    GetComponent<Rigidbody>().velocity = transform.forward * MoveSpeed;
Comment
Add comment · Show 1 · 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 masaegg · Jan 11, 2018 at 01:06 PM 0
Share

Thank you, it's a little bit better than before(the player and the enemies both had rigidbodies already, but I didn't know how to use it in the script, so thank you for that!), but it still doesn't work as it should - the enemies keep going through each other.

Here's a screenshot of two zombies (enemies) overlapping still :

alt text

screen-shot-2018-01-11-at-135956.png (314.7 kB)

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

149 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

Related Questions

Enemy Rigidbody vibration when reaching the player 0 Answers

How to make an enemy ragdoll on death? 1 Answer

I have a model character of terrorist how can in make to animated and come near to the player and attack 2 Answers

Enemy Health Bar Fill (Unity2D) 2 Answers

.SetActive (false) deletes gameObject but it's effects still affect the player 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