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 3kWikiGames · Dec 28, 2018 at 03:57 AM · 2dairigidbody2daddforceenemy ai

Enemy AI 2D not working? ( Rigidbody.Addforce() )

Having trouble getting the enemy ai to chase the character with rigidbody.addforce(). So far it seems to be sliding around the player like its on roller skates and eventually hitting it. I changed it from following using transform.position because I wanted it to also collide with walls. Ill leave my code below, if there's anything to fix it from sliding around I would greatly appreciate it. Thank you!

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Enemy : MonoBehaviour
 {
     public float speed = 10.0f;
     public float diststop = 1.5f;
     public GameObject player;
     private Vector2 target;
     private Vector2 position;
 
     void Start()
     {
         target = new Vector2(0.0f, 0.0f);
         position = gameObject.transform.position;
     }
 
     void Update()
     {
         position = gameObject.transform.position;
         float dist = Vector2.Distance(target, position);
         float x_pos = player.transform.position.x;
         float y_pos = player.transform.position.y;
         target = new Vector2(x_pos, y_pos);
 
         float step = speed * Time.deltaTime;
 
         // move sprite towards the target location
         if (dist >= diststop)
         {
             //transform.position = Vector2.MoveTowards(transform.position, target, step);
             GetComponent<Rigidbody2D>().AddForce((player.transform.position - transform.position) * speed * Time.smoothDeltaTime);
             dist = Vector2.Distance(target, position);
         }
     }
 }
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 toddisarockstar · Dec 28, 2018 at 12:03 PM 0
Share

you may want to change the velocity direction ins$$anonymous$$d of adding force. when you addforce it just adds to the game engines calculations and it figures to slow things down eventually. if you dont like what the phisics engine is doing for you, you can directly controll the movement yourself by changing the velocity every frame. when your object is not moving you can simply set the velocity to zero.

2 Replies

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

Answer by sean244 · Dec 28, 2018 at 06:35 AM

Try this...

 using UnityEngine;

 public class Enemy : MonoBehaviour
 {
 public float speed = 10.0f;
 public float diststop = 1.5f;

 public Transform target;
 private Rigidbody2D rb;

 private Vector2 Position
 {
     get
     {
         return transform.position;
     }
     set
     {
         transform.position = value;
     }
 }

 private void Start()
 {
     rb = GetComponent<Rigidbody2D>();
 }

 private void FixedUpdate()
 {
     float dist = Vector2.Distance(Position, target.position);
     float step = speed * Time.deltaTime;

     if (dist >= diststop)
     {          
         Position = Vector2.MoveTowards(Position, target.position, step);
         rb.MovePosition(Position);
     }
 }
 }
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 3kWikiGames · Dec 28, 2018 at 05:37 PM 0
Share

Worked perfect, thank you so much!

avatar image sean244 3kWikiGames · Dec 28, 2018 at 06:16 PM 0
Share

You’re welcome, but please make note of the changes I made: First of all, I cached the rigidbody. That way, you’re not calling ‘GetComponent’ at every frame - which is horrible for performance. Second of all, I call the rigidbody operation in ‘FixedUpdate’ rather than ‘Update’. This will ensure that it runs consistently across all devices, regardless of the frame rate. Third, I made properties out of ‘Target’ and ‘Position’. That way, you don’t have to constantly set their values in Update. Rather, they will return the values you need from them, only when they are accessed. I hope all of that helped. Please let me know if there’s anything you weren’t clear on.

avatar image
0

Answer by giveson · Dec 30, 2018 at 09:54 PM

you could try: public transform transformofenemy;

void EnemyMove() // any method { transformofenemy.translate;(0, 0, 0) // x y and z }

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

251 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Deteriorating force on object? 0 Answers

Shoot logic, bullet instantiation 1 Answer

How would I move enemies at an angle towards the player, instead of just moving them straight? 0 Answers

Adjust custom PlayerControls to be used by enemies 1 Answer

2D Jump using "Rigidbody2D.AddForce" doesn't work. 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