Search Unity

EnemyFollow script doesn't work

Discussion in 'Scripting' started by rostimosha, Jan 25, 2018.

  1. rostimosha

    rostimosha

    Joined:
    Jan 5, 2018
    Posts:
    5
    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.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Follow : MonoBehaviour {
    6.     public float speed = 5;
    7.     public float waittime = 0.3f;
    8.     Animator anim;
    9.     SpriteRenderer render;
    10.     Light spotlight;
    11.     public float viewdistance;
    12.     float viewangle;
    13. public Transform wayholder;
    14.     public LayerMask layer;
    15.     Transform player;
    16. public Transform rotation;
    17.     Vector3 Dirtoplayer;
    18.     Vector3 lastpos;
    19.     bool canseeplayer = false;
    20.     bool sawplayer = false;
    21.  
    22.     void Start(){
    23.         player = GameObject.Find ("Player").transform;
    24.     Vector3[] waypoints = new Vector3 [wayholder.childCount];
    25.         for (int i = 0; waypoints.Length > i; i++){
    26.             waypoints = wayholder.GetChild(i).position;
    27.         }
    28.         anim = GetComponent<Animator>();
    29.         spotlight = GetComponentInChildren<Light>();
    30.         viewangle = spotlight.spotAngle;
    31.         render = GetComponent<SpriteRenderer>();
    32.         StartCoroutine (FollowPath(waypoints));
    33.     }
    34.     bool cansee(){
    35.         if (Vector3.Distance (transform.position, player.position) < viewdistance) {
    36.             Dirtoplayer = (player.position - transform.position).normalized;
    37.             float anglebtwGrdNPlayer = Vector3.Angle (transform.right, Dirtoplayer);
    38.                 if (anglebtwGrdNPlayer < viewangle / 2f) {
    39.                     if (!Physics2D.Linecast (transform.position, player.position, layer)) {
    40.                         return true;
    41.                 }
    42.             }
    43.         }
    44.         return false;
    45.     }
    46.     void LateUpdate(){
    47.         if (cansee ()) {
    48.             canseeplayer = true;
    49.         }
    50.         else{
    51.             canseeplayer = false;
    52.     }
    53.     }
    54.  
    55.     IEnumerator FollowPath(Vector3[] waypoints){
    56.         transform.position = waypoints [0];
    57.         int targetwayindex = 1;
    58.         if (canseeplayer == true) {
    59.             yield return new WaitForSeconds (0.3f);
    60.         }
    61.             Vector3 targetway = waypoints [targetwayindex];
    62.  
    63.         while(true){
    64.             if (canseeplayer == true) {
    65.                     lastpos = player.position;
    66.                     transform.position = Vector3.MoveTowards (transform.position, lastpos, speed * Time.deltaTime);
    67.                 Debug.Log("pidor confirmed");
    68.                 sawplayer = true;
    69.                 }
    70.             if (sawplayer == false) {
    71.                 transform.position = Vector3.MoveTowards (transform.position, targetway, speed * Time.deltaTime);
    72.                 Debug.Log ("pidor undetected");
    73.                 if (transform.position == targetway) {
    74.                     targetwayindex = (targetwayindex + 1) % waypoints.Length;
    75.                     targetway = waypoints [targetwayindex];
    76.                     yield return new WaitForSeconds (waittime);
    77.                 }
    78.        
    79.             }
    80.             yield return null;
    81.     }    }
    82. }
     
    Last edited: Jan 25, 2018
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    rostimosha likes this.
  3. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    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.
     
  4. rostimosha

    rostimosha

    Joined:
    Jan 5, 2018
    Posts:
    5
    Actually cansee function works nicely, i have problem with movement. Idk why, but when he sees player - he stops and do nothing
     
  5. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    How often does the log print when he sees the player? Always? No errors or anything?
     
  6. rostimosha

    rostimosha

    Joined:
    Jan 5, 2018
    Posts:
    5
    Yes, always, no errors.
     
  7. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Well, I'm stumped. It seems as though that code should move something each frame by speed.