Hey guys!
I am pretty new to Unity and C# so I wanted to ask how it is possible to make my enemy stop moving when the player comes close.
My enemy moves this way:
if (moveRight)
{
transform.Translate(2 * Time.deltaTime * speed, 0, 0);
transform.localScale = new Vector2(1, 1);
}
else
{
transform.Translate(-2 * Time.deltaTime * speed, 0, 0);
transform.localScale = new Vector2(-1, 1);
}
if (trig.gameObject.CompareTag("stopper"))
{
if (moveRight)
{
moveRight = false;
}
else
{
moveRight = true;
The trigger is there because I created two GameObjects my enemy collides to so that he turns. now I want to make him stop when the player comes close.
I found a solution like this:
Vector2.Distance(transform.position, player.position
but it didn’t to the job.
I hope you can help me out 
Kind regards
2 Answers
2
@XBigGuyX Vector2.Distance gives distance between two point so you need to do something like this:
if(Vector2.Distance(transform.position, player.position) < 1){
//Stop Moving
}
The correct method is to set up waypoints. So the enemy is not limited to the X axis. For a simple patrol, 2 points are enough.
A waypoint is an empty object on the map. One of the methods (there are others, but I take the simplest) is to place the WPs by hand in the Inspector, so the script knows where they are.
The code is incomplete: it does not deal with the attack. The enemy simply goes to the player’s position.
using UnityEngine;
public class Enemy : MonoBehaviour
{
[SerializeField] private Transform[] waypoints;
[SerializeField] private float speed = 5f;
[SerializeField] private Transform player = null;
private enum State { Patrol, Attack };
private State state;
private int currentWayPointIndex = 0;
private void Start ()
{
state = State.Patrol;
}
private void Update ()
{
switch (state)
{
case State.Patrol:
OnPatrol ();
break;
case State.Attack:
Attack ();
break;
default:
break;
}
}
private bool IsPlayerClose ()
{
// Compute distance has a heavy cost : a square root is computed (at each frame!). So I use squarred distance :
// I don't need the exact distance, just whether or not it's smaller than a certain value.
float squarredDistance = (player.position - transform.position).sqrMagnitude;
// As the value I chose 3 meters, so I compare against 3 ^ 2. It is to be adjusted according to the scale
// of your game and the type of enemy (melee, ranged, ...)
return (squarredDistance <= 9f);
}
private void OnPatrol ()
{
// Move to the next Waypoint.
transform.position = Vector3.MoveTowards (transform.position, waypoints[currentWayPointIndex].position, speed * Time.deltaTime);
// Is the player close?
if (IsPlayerClose ())
{
// Attack!!!
state = State.Attack;
}
// Arrived?
float squarredDistance = (waypoints[currentWayPointIndex].position - transform.position).sqrMagnitude;
if (squarredDistance <= 1f)
{
NextWaypoint ();
}
}
private void Attack ()
{
// Move to the player.
transform.position = Vector3.MoveTowards (transform.position, player.position, speed * Time.deltaTime);
// Like above, check the distance and make the job.
// Your code.
}
private void NextWaypoint ()
{
currentWayPointIndex++;
if (currentWayPointIndex >= waypoints.Length)
{
currentWayPointIndex = 0;
}
}
}
And how would I make him stop moving in this case? I don't know how the "Stop Moving" code would look like :D
– XBigGuyX