[ad_1]
I’m presently utilizing A stars’ pathfinding code right here: https://arongranberg.com/astar/. So I did not create any scripts, and used the scripts offered by astar. However the issue right here is that regardless that my participant may be very far-off from my enemy, the enemy will nonetheless transfer in direction of the participant, which is one thing I do not need. I would like the enemy to solely transfer in direction of the participant as soon as the participant is a sure distance from the enemy. Additionally, I’m working in unity 2nd. Can anybody assist me make a code for this?
Edit: I’ve made a script that makes the enemy pathfind utilizing Brackeys tutorial, after which I manually added a code that checks the gap between the participant and the enemy, and if the gap is one thing like 20f, then drive can be added to the enemy. However in some way it nonetheless does not work, so right here is the code:
utilizing System.Collections;
utilizing System.Collections.Generic;
utilizing UnityEngine;
utilizing Pathfinding;
public class EnemyAI : Enemy
{
public Remodel goal;
public Remodel eagle;
public float PlayerToEnemyDistance;
public float velocity = 200f; //velocity of eagle
public float nextWaypointDistance = 3f; //the gap of the following waypoint
Path path; //the present path we're following
int currentWaypoint = 0; //the present waypoint alongside that path we're targetting
bool reachedEndOfPath = false; //checks whether or not the eagle has reached the top of path or not
Seeker seeker; //references our seeker script
personal Rigidbody2D rb;
void Begin()
{
seeker = GetComponent<Seeker>(); //referencing seeker script
rb = rework.GetComponent<Rigidbody2D>(); //referencing rigidbody
InvokeRepeating("UpdatePath", 0f, .5f); //permits the eagle to maintain producing path
rb.gravityScale = 0.000001f;
velocity = 0f;
}
void UpdatePath()
{
if(seeker.IsDone()) //so what a number of paths will not be producing directly
seeker.StartPath(rb.place, goal.place, OnPathComplete);
//generates a path
//first phrase in bracket is beginning place
//second phrase in bracket is ending place
//third phrase in bracket is a operate we name after we end calculating the trail
}
void OnPathComplete(Path p) //generate a path known as p
{
if (!p.error) //if we did not get an error for the trail
{
path = p; //set present path to newly generate path known as p
currentWaypoint = 0; //resets progress alongside the trail
}
}
// Replace is known as as soon as per body
void FixedUpdate()
{
PlayerToEnemyDistance = (goal.place.x - eagle.place.x);
//cheks the gap between the participant and eagle
if (path == null) //if there is no such thing as a path, return
return;
if (currentWaypoint >= path.vectorPath.Rely) //checks whether or not eagle has reached the top waypoint
{
reachedEndOfPath = true; //reaches finish of path
return;
}
else
{
reachedEndOfPath = false; //does not attain finish of path
}
//get the route of the following waypoint alongside our path
Vector2 route = ((Vector2)path.vectorPath[currentWaypoint] - rb.place).normalized;
Vector2 drive = route * velocity * Time.deltaTime;
if(PlayerToEnemyDistance <= 30)
{
rb.gravityScale = 0f;
velocity = 300f;
rb.AddForce(drive); //provides drive
}
//the under line checks the gap of the following waypoint
float distance = Vector2.Distance(rb.place, path.vectorPath[currentWaypoint]);
if(distance < nextWaypointDistance)
{
currentWaypoint++;
//we have now reached our present waypoint and able to transfer on to the following one
}
if (rb.velocity.x >= 0.01f) //flip sprites
{
eagle.localScale = new Vector3(-1f, 1f, 1f);
}
else if (rb.velocity.x <= -0.01f) //flip sprites
{
eagle.localScale = new Vector3(1f, 1f, 1f);
}
}
}
[ad_2]