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 Brunomir · Mar 27, 2015 at 05:46 AM · 2dpathfinding2d-physicstopdownrpg

2D TopDown RPG need help making RigidBody2D AI move at a same speed

I want to make the AI move at the same speed when its chasing the player with this free a* pathfinidg code that i'm using (the A* Pathfinding Project - Arongranberg.com). Thing is that the AI it kind of jumps from one spot to the next one, and in addition it slows down when its getting near the player or when its trying to go around a wall to get the player. And after a while he suddenly stops following the player. Or seeker / target insted of enemy and player.

The error that i get and the values of the seeker and enemy AI are the following :

alt text

If you need me to explain something else for you to understand, please let me know. I'm really stuck here. I've tried changing the force mode and other stuff. Here's the c# code: public Transform target;

      public float updateRate = 2f;    
      
      private Seeker seeker;
  
      private Rigidbody2D rb;    
  
      public Path path;    
      
      public float speed = 300f;
  
      public ForceMode2D fMode;
      
      [HideInInspector]
      public bool pathIsEnded = false;    
      
      public float nextWaypointDistance = 3;    
      
      private int currentWaypoint = 0;
  
      private bool searchingForPlayer = false;
  
      void Start () {
          seeker = GetComponent<Seeker>();
          rb = GetComponent<Rigidbody2D>();
  
          if (target == null) {
              if (!searchingForPlayer){
                  searchingForPlayer = true;
                  StartCoroutine (SearchForPlayer());
              }
              return;
          }
          
          
          seeker.StartPath (transform.position, target.position, OnPathComplete);
          
          StartCoroutine (UpdatePath ());
      }
  
      IEnumerator SearchForPlayer(){
          GameObject sResult = GameObject.FindGameObjectWithTag("Player");
          if(sResult == null){
              yield return new WaitForSeconds (0.5f);
              StartCoroutine (SearchForPlayer());
          } else {
              target = sResult.transform;
              searchingForPlayer = false;
              StartCoroutine (UpdatePath());
              return false;
          }
      }
  
      IEnumerator UpdatePath () {
          if (target == null) {
              if (!searchingForPlayer){
                  searchingForPlayer = true;
                  StartCoroutine (SearchForPlayer());
              }
              return false;
          }
          
          
          seeker.StartPath (transform.position, target.position, OnPathComplete);
          
          yield return new WaitForSeconds ( 1f/updateRate );
          StartCoroutine (UpdatePath());
      }
      
      public void OnPathComplete (Path p) {
          Debug.Log ("We got a path. Did it have an error? " + p.error);
          if (!p.error) {
              path = p;
              currentWaypoint = 0;
          }
      }
      
      void FixedUpdate () {
          if (target == null) {
              if (!searchingForPlayer){
                  searchingForPlayer = true;
                  StartCoroutine (SearchForPlayer());
              }
              return;
          }
          
          
          
          if (path == null)
              return;
          
          if (currentWaypoint >= path.vectorPath.Count) {
              if (pathIsEnded)
                  return;
              
              Debug.Log ("End of path reached.");
              pathIsEnded = true;
              return;
          }
          pathIsEnded = false;
      
          
          Vector3 dir = ( path.vectorPath[currentWaypoint] - transform.position ).normalized;
          dir *= speed * Time.fixedDeltaTime;
          
          //Move the AI (IDK if i should use force, velocity or something else to make it move allways at the same speed, or perhaps the issue is somewhere else on the code)
          rb.AddForce (dir, fMode);
     
  
          float dist = Vector3.Distance (transform.position, path.vectorPath[currentWaypoint]);
          if (dist < nextWaypointDistance) {
              currentWaypoint++;
              return;
          }
      }
error-unity.jpg (202.3 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

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

New To Unity: 2D Top Down Tricks 0 Answers

2D Pathfinding Top Down 0 Answers

8-directional orientation, top down 2D, seperate from movement 0 Answers

2D top down shell ejection 1 Answer

2D Directional top down movement,Topdown 2d Directional Movement 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