Search Unity

Still can't get it to work (updated code)

Discussion in 'Scripting' started by SuperCrow2, Jun 17, 2018.

  1. SuperCrow2

    SuperCrow2

    Joined:
    Mar 8, 2018
    Posts:
    584
    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.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class EnemyMovement : MonoBehaviour
    6. {
    7.     public float followDistance;
    8.     public Transform Player;
    9.     public Transform target;
    10.    
    11.  
    12. [SerializeField]
    13.  
    14.     Transform[] waypoints;
    15.  
    16.  
    17.  
    18.     [SerializeField]
    19.     float moveSpeed = 5f;
    20.     int waypointIndex = 0;
    21.    
    22.  
    23.     private void Start()
    24.  
    25.     {
    26.         transform.position = waypoints[waypointIndex].transform.position;
    27.         target = GameObject.Find("Player").transform;
    28.     }
    29.  
    30.     private void Update()
    31.     {
    32.         Move();
    33.         follow();
    34.  
    35.     }
    36.  
    37.  
    38.  
    39.     private void follow()
    40.     {
    41.  
    42.  
    43.  
    44.      
    45.         if (Vector3.Distance(Player.transform.position, transform.position) < followDistance)
    46.        
    47.  
    48.             follow();
    49.        
    50.  
    51.         else Move();
    52.     }
    53.  
    54.  
    55.  
    56.  
    57.     private void Move()
    58.  
    59.  
    60.     {
    61.         transform.position = Vector3.MoveTowards(transform.position, waypoints[waypointIndex].transform.position, moveSpeed * Time.deltaTime);
    62.  
    63.         if (transform.position == waypoints[waypointIndex].transform.position)
    64.  
    65.  
    66.             waypointIndex += 1;
    67.  
    68.         if (waypointIndex == waypoints.Length)
    69.             waypointIndex = 0;
    70.     }
    71. }
    72.    
     
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    This is recursive:
    Code (csharp):
    1.  
    2. [LIST=1]
    3. [*]    private void follow()
    4. [*]    {
    5. [*]
    6.  
    7. [*]
    8.  
    9. [*]
    10.  
    11. [*]  
    12. [*]        if (Vector3.Distance(Player.transform.position, transform.position) < followDistance)
    13. [*]    
    14. [*]
    15.  
    16. [*]            follow();
    17. [*]    
    18. [*]
    19.  
    20. [*]        else Move();
    21. [*]    }
    22. [/LIST]
    23.  
    It doesn't follow the player. You need something like:
    Code (csharp):
    1.  
    2. private void Update(){
    3. if (Vector3.Distance(Player.transform.position, transform.position) < followDistance){
    4. follow();
    5. }
    6. else Move();
    7. }
    8. private void follow(){
    9. transform.position = Vector3.MoveTowards(transform.position,Player.position,moveSpeed * Time.deltaTime);
    10.  
    11. }
    12. //move definition is the same
    13.  
    You should really think about doing a few more tutorials, but good luck.
     
  3. SuperCrow2

    SuperCrow2

    Joined:
    Mar 8, 2018
    Posts:
    584
    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

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. //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
    6.  
    7. public class EnemyMovement : MonoBehaviour
    8. {
    9.     public float followDistance;
    10.     public Transform Player;
    11.     public Transform target;
    12.  
    13.  
    14.  
    15.  [SerializeField]
    16.  
    17.     Transform[] waypoints;
    18.  
    19.  
    20.  
    21.     [SerializeField]
    22.     float moveSpeed = 5f;
    23.     int waypointIndex = 0;
    24.  
    25.  
    26.  
    27.     private void Start()
    28.  
    29.     {
    30.         transform.position = waypoints[waypointIndex].transform.position;
    31.         target = GameObject.Find("Player").transform;
    32.     }
    33.  
    34.     private void Update()
    35. {
    36.         if (Vector3.Distance(Player.transform.position, transform.position) < followDistance)
    37.         {
    38.             follow();
    39.         }
    40.         else Move();
    41.  
    42.   }
    43.  
    44.     private void follow()
    45.     {
    46.         transform.position = Vector3.MoveTowards(transform.position, Player.position, moveSpeed * Time.deltaTime);
    47.  
    48.     }
    49.  
    50.     private void Move()
    51.  
    52.  
    53.  
    54. {
    55.         transform.position = Vector3.MoveTowards(transform.position, waypoints[waypointIndex].transform.position, moveSpeed * Time.deltaTime);
    56.  
    57.         if (transform.position == waypoints[waypointIndex].transform.position)
    58.  
    59.  
    60.             waypointIndex += 1;
    61.  
    62.         if (waypointIndex == waypoints.Length)
    63.             waypointIndex = 0;
    64.     }
    65. }
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    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?
     
  5. SuperCrow2

    SuperCrow2

    Joined:
    Mar 8, 2018
    Posts:
    584
    Nope
     
  6. CubicCBridger

    CubicCBridger

    Joined:
    Apr 19, 2017
    Posts:
    44
    It may be that this line here

    Code (CSharp):
    1. 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.
     
  7. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Brathnann was telling you what you should do next in the form of a rhetorical question.
     
    Kurt-Dekker likes this.
  8. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Exactly, you should never use == comparisons for float types.
     
  9. SuperCrow2

    SuperCrow2

    Joined:
    Mar 8, 2018
    Posts:
    584
    The closest thing I can find to what I need to happen is this:


    But idk if they will work for me or not.