- Home /
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 :
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;
}
}
Your answer
Follow this Question
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