Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question The Movetowards function is just placing my object at (0,0) instead of the target.

Discussion in 'Scripting' started by HelloThere747, Jul 9, 2023.

  1. HelloThere747

    HelloThere747

    Joined:
    Jul 12, 2021
    Posts:
    2
    So I'm trying to make a script to move a person around to random locations, basically wandering, by giving the x and y ints random values and then making a target vector out of those two values. Then the Move to point function is called which is supposed to make the person move to the target. If the person is at the target then "atDestination" is equal to true. If that bool is true then it calls the Start function basically starting the entire thing again. I really don't know what is wrong, whether just one thing, or just the entire way I'm trying to do this.\
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Movement : MonoBehaviour
    6. {
    7.     Vector2 currentPosition;
    8.     Vector2 randomTarget;
    9.     int randomX, randomY;
    10.     public float speed;
    11.  
    12.     public bool wanderNow;
    13.     bool atDestination;
    14.  
    15.     void Start()
    16.     {
    17.         bool wanderNow = true;
    18.     }
    19.  
    20.     void Update()
    21.     {
    22.         Vector2 currentPosition = transform.position;
    23.         if(wanderNow == true)
    24.         {
    25.             GenerateRandomVector();
    26.         }
    27.     }
    28.     void GenerateRandomVector()
    29.     {
    30.         int randomX = Random.Range(-17, 17);
    31.         int randomY = Random.Range(-9, 9);
    32.         Vector2 randomTarget = new Vector2(randomX, randomY);
    33.         MoveToPoint();
    34.     }
    35.  
    36.     void MoveToPoint()
    37.     {
    38.         wanderNow = false;
    39.         transform.position = Vector2.MoveTowards(currentPosition, randomTarget, speed * Time.deltaTime);
    40.        
    41.         if(currentPosition == randomTarget)
    42.         {
    43.             atDestination = true;
    44.         }
    45.         else
    46.         {
    47.             atDestination = false;
    48.         }
    49.  
    50.         if (atDestination == true)
    51.         {
    52.             Start();
    53.         }
    54.  
    55.     }
    56. }
    57.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    I see that line 39 assigns the newly-calculated position directly to the
    transform.position
    .

    But you never update
    currentPosition
    !

    You should instead assign it back to
    currentPosition
    , THEN assign
    currentPosition
    to the
    transform.position
    .

    Otherwise you'll never "leave" where currentPosition is by more than a tiny smidge.
     
  3. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    510
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class RandomMovement : MonoBehaviour
    6. {
    7.     Vector2 target;
    8.  
    9.     void Start()
    10.     {
    11.         target=transform.position;
    12.     }
    13.  
    14.     void GenerateRandomTarget()
    15.     {
    16.         int randomX = Random.Range(-17, 17);
    17.         int randomY = Random.Range(-9, 9);
    18.         target = new Vector2(randomX, randomY);
    19.     }
    20.  
    21.     void Update()
    22.     {
    23.         transform.position=Vector2.MoveTowards(transform.position,target,Time.deltaTime);
    24.         if (transform.position == target)  
    25.         {
    26.             GenerateRandomTarget();
    27.         }
    28.     }
    29. }
    30.  
     
    Bunny83 likes this.
  4. HelloThere747

    HelloThere747

    Joined:
    Jul 12, 2021
    Posts:
    2
    Could you explain exactly how to do it.
     
  5. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    I'm not sure how much clearer he needed to be.

    Here:
    Code (CSharp):
    1. currentPosition = Vector2.MoveTowards(currentPosition, randomTarget, speed * Time.deltaTime);
    2. transform.position = currentPosition;
     
    Kurt-Dekker likes this.