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 Pigpower6 · Jun 19, 2021 at 05:45 PM · collsion

Enemy following AI doesnt collide.

Hi, Im currently working on a 2d rpg game. I have a enemy script that is following the player with Vector2.MoveTowards. But my problem is that the enemies ignore collsion with objects when the player is behind the object. Is there a way to improve my follow script or can i create my own collision?

follow scipt:`if (Vector2.Distance(transform.position, playerPos.position) < distance){ transform.position = Vector2.MoveTowards(transform.position, playerPos.position, speedEnemy * Time.deltaTime); } else{

             if (Vector2.Distance(transform.position, currentPos) <= 0){
            
             }
             else{
                 transform.position = Vector2.MoveTowards(transform.position, currentPos, speedEnemy * Time.deltaTime);
             }
         }`

Comment
Add comment · Show 1
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 logicandchaos · Jun 19, 2021 at 07:51 PM 0
Share

I'm not sure what you mean they ignore collisions, are you not using colliders and rigidbodies?

1 Reply

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

Answer by Tinglee82 · Jun 20, 2021 at 07:08 AM

If you want to move a transform while taking into account scene collisions, you shouldn't set transform.position because that negates all the physics. I assume that you have both Collider and RigidBody components added to your enemy object. You could try using the MovePosition in Rigidbody instead:

 public class YourEnemyClass : MonoBehaviour
 {
     public float speed;
     private Rigidbody rb;
     
     
     public void Start()
     {
         rb = GetComponent<Rigidbody>();
     }
     
     public void Update()
     {
         if (Vector2.Distance(transform.position, currentPos) <= 0){
             
         }
         else{
             var _targetPosition = Vector2.MoveTowards(transform.position, currentPos, speedEnemy * Time.deltaTime);
             rb.MovePosition(_targetPosition);
         }
     }
 }
 
Comment
Add comment · 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 Pigpower6 · Jun 20, 2021 at 11:01 AM 1
Share

You dont know how much this helped me:) Thank you so much!!

avatar image Tinglee82 Pigpower6 · Jun 20, 2021 at 03:31 PM 0
Share

my pleasure!

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

120 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

Related Questions

Collison Kills 1 Answer

Objects side by side without triggering the OnTrigger 1 Answer

Collision resolution in tight spaces 1 Answer

Destroy on collision wont work with mesh collider 1 Answer

How can I keep enemies that follow my Player, from becoming a "blob"? 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