EnemyFollow script doesn't work

This is the script, that makes Enemy Follow path. When the enemy can see player - he is follow the player. But in game it works not correctly. Enemy follow path, but when he sees player - he stops. Please help me.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Follow : MonoBehaviour {
    public float speed = 5;
    public float waittime = 0.3f;
    Animator anim;
    SpriteRenderer render;
    Light spotlight;
    public float viewdistance;
    float viewangle;
public Transform wayholder;
    public LayerMask layer;
    Transform player;
public Transform rotation;
    Vector3 Dirtoplayer;
    Vector3 lastpos;
    bool canseeplayer = false;
    bool sawplayer = false;

    void Start(){
        player = GameObject.Find ("Player").transform;
    Vector3[] waypoints = new Vector3 [wayholder.childCount];
        for (int i = 0; waypoints.Length > i; i++){
            waypoints = wayholder.GetChild(i).position;
        }
        anim = GetComponent<Animator>();
        spotlight = GetComponentInChildren<Light>();
        viewangle = spotlight.spotAngle;
        render = GetComponent<SpriteRenderer>();
        StartCoroutine (FollowPath(waypoints));
    }
    bool cansee(){
        if (Vector3.Distance (transform.position, player.position) < viewdistance) {
            Dirtoplayer = (player.position - transform.position).normalized;
            float anglebtwGrdNPlayer = Vector3.Angle (transform.right, Dirtoplayer);
                if (anglebtwGrdNPlayer < viewangle / 2f) {
                    if (!Physics2D.Linecast (transform.position, player.position, layer)) {
                        return true;
                }
            }
        }
        return false;
    }
    void LateUpdate(){
        if (cansee ()) {
            canseeplayer = true;
        }
        else{
            canseeplayer = false;
    }
    }

    IEnumerator FollowPath(Vector3[] waypoints){
        transform.position = waypoints [0];
        int targetwayindex = 1;
        if (canseeplayer == true) {
            yield return new WaitForSeconds (0.3f);
        }
            Vector3 targetway = waypoints [targetwayindex];

        while(true){
            if (canseeplayer == true) {
                    lastpos = player.position;
                    transform.position = Vector3.MoveTowards (transform.position, lastpos, speed * Time.deltaTime);
                Debug.Log("pidor confirmed");
                sawplayer = true;
                }
            if (sawplayer == false) {
                transform.position = Vector3.MoveTowards (transform.position, targetway, speed * Time.deltaTime);
                Debug.Log ("pidor undetected");
                if (transform.position == targetway) {
                    targetwayindex = (targetwayindex + 1) % waypoints.Length;
                    targetway = waypoints [targetwayindex];
                    yield return new WaitForSeconds (waittime);
                }
       
            }
            yield return null;
    }    }
}

Please read this page for how to post code nicely on the forums and edit your post: Using code tags properly

1 Like

You want it to follow the player instead of the path, I imagine, right?

Separately, I can point out that Physics2D.Lineccast returns a RaycastHit2D. You should check the collider(s) for not null, rather than a bool, as you’re doing.

Actually cansee function works nicely, i have problem with movement. Idk why, but when he sees player - he stops and do nothing

How often does the log print when he sees the player? Always? No errors or anything?

Yes, always, no errors.

Well, I’m stumped. It seems as though that code should move something each frame by speed.