Search Unity

Make character follow the player

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

  1. SuperCrow2

    SuperCrow2

    Joined:
    Mar 8, 2018
    Posts:
    584
    Hey folks, so I got a character to move along way points, I will show you the code and from that code, is it possible to make the character follow you if you are in a certain distance of it and then when out of that distance the character goes back to continue moving on its path? If it's possible what do I add and where does it go? This is in Unity 2D

    Code (CSharp):
    1.    [SerializeField]
    2.  
    3.     Transform[] waypoints;
    4.  
    5.     [SerializeField]
    6.     float moveSpeed = 5f;
    7.  
    8.     int waypointIndex = 0;
    9.  
    10.      private void Start()
    11.      
    12.     {
    13.         transform.position = waypoints[waypointIndex].transform.position;
    14.     }
    15.  
    16.    private void Update()
    17.     {
    18.         Move();
    19.     }
    20.  
    21.     private void Move()
    22.     {
    23.         transform.position = Vector3.MoveTowards(transform.position, waypoints[waypointIndex].transform.position, moveSpeed * Time.deltaTime);
    24.  
    25.         if (transform.position == waypoints[waypointIndex].transform.position) {
    26.      
    27.             waypointIndex += 1;
    28.         }
    29.         if (waypointIndex == waypoints.Length)
    30.             waypointIndex = 0;
    31.     }
    32. }
    33.  
    Btw the code works I just left out a couple of characters from the code.
     
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    You would just have a follow method and then in your update:
    Code (csharp):
    1.  
    2. if(Distance(character.transform.position,transform.position < followDistance){
    3. Follow();
    4. }
    5. else Move();
    6.  
     
  3. SuperCrow2

    SuperCrow2

    Joined:
    Mar 8, 2018
    Posts:
    584
    Thanks but where would I put in the distance value like lets say it only follows you when you are 1-3 spaces (idk if it's called spaces lol) away from it?
     
  4. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    It's a variable so you make it public and at the top of the class. You can't use spaces. It's a vector2 method:
    https://docs.unity3d.com/ScriptReference/Vector2.Distance.html.
    You can just experiment and find the right amount. I should have written
    Code (csharp):
    1.  
    2. if(Vector2.Distance(//same as above){}
    3.  
     
  5. SuperCrow2

    SuperCrow2

    Joined:
    Mar 8, 2018
    Posts:
    584
    I know I had to change it from Vector2 to Vector3 to get the object to move along the way points instead of just jumping to one and then stop moving. It must of been some Unity update that caused Vector2 to not work anymore because I did exactly what the tutorial did and that tutorial used Vector2.
     
  6. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Probably use Vector3, then. I don't work in 2d very much. There isn't even an example in the manual for Vector2.Distance.
     
  7. SuperCrow2

    SuperCrow2

    Joined:
    Mar 8, 2018
    Posts:
    584
    it says i cant use this sign "<" with Vector3 and float, I have public float followDistance;

    here's what I have:

    Code (CSharp):
    1.  public float followDistance;
    2.  
    3.  
    4.  
    5.  
    6.    
    7.     [SerializeField]
    8.  
    9.     Transform[] waypoints;
    10.  
    11.  
    12.  
    13.     [SerializeField]
    14.     float moveSpeed = 5f;
    15.  
    16.     int waypointIndex = 0;
    17.  
    18.  
    19.  
    20.  
    21.     private void Start()
    22.  
    23.     {
    24.         transform.position = waypoints[waypointIndex].transform.position;
    25.     }
    26.  
    27.     private void Update()
    28.     {
    29.         Move();
    30.         follow();
    31.  
    32.     }
    33.  
    34.  
    35.  
    36.     private void follow()
    37.  
    38.     {
    39.  
    40.  
    41.  
    42.         object character = null;
    43.         if (Vector3.Distance(character.transform.position,transform.position< followDistance){
    44.  
    45.             follow();
    46.         }
    47.  
    48.         else Move();
    49.     }
     
  8. Tattomoosa

    Tattomoosa

    Joined:
    Jan 21, 2018
    Posts:
    2
    On this line:

    Code (CSharp):
    1. if (Vector3.Distance(character.transform.position,transform.position< followDistance){
    You need to add a ')' after transform.position:

    Code (CSharp):
    1. if (Vector3.Distance(character.transform.position,transform.position) < followDistance){
    What's happening here is that Vector3.Distance is a function like Start (or your function 'follow') that takes two transform.positions and returns the distance between them in a float. Since you didn't close the parentheses, the function tried to evaluate 'transform.position<followDistance' as the second argument, hence your error.
     
  9. SuperCrow2

    SuperCrow2

    Joined:
    Mar 8, 2018
    Posts:
    584
    Thanks, that fixed one error. Now just one last error (I forgot about this error). There's a red underline under the first "transform" (character.transform)

    "object does not contain a definition for 'transform' and no extension method 'transform' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)"

    When I remove this: object character = null; the red underline is gone under "transform" but instead is under "character" instead.