- Home /
Enemy chase player and do not stop until it reaches the target?
Hey, I will get to the point : I have script that makes Enemy move towards my Player (it is 2D platformer). Enemy move if distance between Player and object is less than max dist. but if I reach higher value with my Player than max dist Enemy stops following.
if (Vector2.Distance(transform.position, Player.transform.position) <= 10)
{
Vector2 enemyVelocity = new Vector2((transform.position.x - Player.transform.position.x) * Speed, 0);
rgBody2d.velocity = -enemyVelocity.normalized * Speed;
anim.SetFloat("Speed", enemyVelocity.magnitude);
}
How can I make follow my player if it shows in Camera and do not stop until it reaches the Player? Anyone who can explain?
Hmm, not sure if i got what you trying to do, but if you just want the enemy to follow the player forever afeter it saw him, you could set a boolean to true if the distance is less then your 10, if this boolean is true, the enemy will move towards the player no matter the distance.
private bool followPlayer = false;
if (Vector2.Distance(transform.position, Player.transform.position) <= 10)
{
followPlayer = true;
}
if(followPlayer)
{
//$$anonymous$$ove towards the player.
}
Hi, thanks for your answer! I've already tried this solution earlier but it doesn't work. If think this is because it sets followPlayer to true if the distance is <= 10 so if the distance is greater than 10 it sets followPlayer to false and the second if statement doesn't work.
Any other suggestions? Thank you again.
Answer by mrpmorris · Jul 28, 2016 at 08:33 AM
In your behavior add the following
void OnBecameVisible() {
enabled = true;
}
void OnBecameInvisible() {
enabled = false;
}
If that isn't sufficient (because the sprite gets hidden when on screen) try checking the sprite's position against one of the following
Screen.width
Camera.pixelWidth
I did something like this but it doesn't seem to work..
void OnBecameVisible()
{
enabled = true;
}
void OnBecameInvisible()
{
enabled = false;
}
void chasePlayer()
{
Vector2 enemyVelocity = new Vector2((transform.position.x - Player.transform.position.x) * Speed, 0);
rgBody2d.velocity = -enemyVelocity.normalized * Speed;
anim.SetFloat("Speed", enemyVelocity.magnitude);
}
void enemyPatrol()
{
if (transform.position == patrolPoints[currentPoint].position)
{
transform.localScale = new Vector2(-transform.localScale.x, transform.localScale.y);
currentPoint++;
}
if (currentPoint >= patrolPoints.Length)
{
currentPoint = 0;
}
transform.position = Vector3.$$anonymous$$oveTowards(transform.position, patrolPoints[currentPoint].position, moveSpeed = Time.deltaTime);
anim.SetFloat("Speed", transform.position.magnitude);
}
void Update()
{
if (enabled)
{
chasePlayer();
}
if (!enabled)
{
enemyPatrol();
}
"It doesn't work" isn't going to tell anyone enough to help you
Yeah, my bad, sorry. So I tried to read docs about OnBecameVisible but I am not sure how to use it. Is it possible to take Screen.width and check if my Enemy is somewhere between left edge and right edge of the screen?
Answer by maksymdev · Jul 28, 2016 at 07:58 PM
@mrpmorris so I got this working for me :D
float leftBorder = Camera.main.ViewportToWorldPoint(new Vector2(0, 0)).x;
float rightBorder = Camera.main.ViewportToWorldPoint(new Vector2(1, 0)).x;
if (leftBorder <= transform.position.x && rightBorder >= transform.position.x)
{
chasePlayer();
}
else
{
enemyPatrol();
}
Thank you very much for help and navigating me!
Your answer

Follow this Question
Related Questions
How do I make a enemy follow me 3 Answers
Basic enemy AI for 2.5D Platormer 1 Answer
AI & pathfinding for 2D platformer - how to approach? 0 Answers
How do I have multiple AI that follow the player but don't move into each other? 1 Answer
Make player not be seen by AI, when player in foilage and shadows. 1 Answer