I posted a thread about a different problem earlier but that was fixed and so to not make it confusing for anyone I will make a new thread that has the updated code. So the enemy follows a path along way points and I’ve been trying to get the enemy to follow the player when the player is in a certain distance of the enemy but when out of that distance the enemy goes back to it’s path. I can’t seem to make this work.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyMovement : MonoBehaviour
{
public float followDistance;
public Transform Player;
public Transform target;
[SerializeField]
Transform[] waypoints;
[SerializeField]
float moveSpeed = 5f;
int waypointIndex = 0;
private void Start()
{
transform.position = waypoints[waypointIndex].transform.position;
target = GameObject.Find("Player").transform;
}
private void Update()
{
Move();
follow();
}
private void follow()
{
if (Vector3.Distance(Player.transform.position, transform.position) < followDistance)
follow();
else Move();
}
private void Move()
{
transform.position = Vector3.MoveTowards(transform.position, waypoints[waypointIndex].transform.position, moveSpeed * Time.deltaTime);
if (transform.position == waypoints[waypointIndex].transform.position)
waypointIndex += 1;
if (waypointIndex == waypoints.Length)
waypointIndex = 0;
}
}
I am beginning to feel like you just can’t do it. This caused them to not move. And I can’t find tutorials for this
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//trying to get it so the enemy follows you when you are in a certain distance from it but then when outside of that distance the enemy goes back its way point to continue moving
public class EnemyMovement : MonoBehaviour
{
public float followDistance;
public Transform Player;
public Transform target;
[SerializeField]
Transform[] waypoints;
[SerializeField]
float moveSpeed = 5f;
int waypointIndex = 0;
private void Start()
{
transform.position = waypoints[waypointIndex].transform.position;
target = GameObject.Find("Player").transform;
}
private void Update()
{
if (Vector3.Distance(Player.transform.position, transform.position) < followDistance)
{
follow();
}
else Move();
}
private void follow()
{
transform.position = Vector3.MoveTowards(transform.position, Player.position, moveSpeed * Time.deltaTime);
}
private void Move()
{
transform.position = Vector3.MoveTowards(transform.position, waypoints[waypointIndex].transform.position, moveSpeed * Time.deltaTime);
if (transform.position == waypoints[waypointIndex].transform.position)
waypointIndex += 1;
if (waypointIndex == waypoints.Length)
waypointIndex = 0;
}
}
Personally I would look into navmesh, but if you don’t want to use that…
The simple question is…Did you put debug messages in your code to see where it was hitting or not hitting so you could see perhaps where the issue was?
if (transform.position == waypoints[waypointIndex].transform.position)
is never triggering due to rounding errors, i.e it’s positional vector2 may never EXACTLY the same but within 0.0001 or whatever. You could fix this by checking its within 0.1f or something like that. (Like @Brathnann said, use debug logs to find out which branches are being hit and which aren’t).
I also agree with @Brathnann that you should look into tutorials on navmeshes, they’re designed to provide the functionality of exactly what you are trying to do along with other features so you aren’t trying to reinvent the wheel.