Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

MoveTowards not causing the object to move

Discussion in 'Scripting' started by Bifflestein, Dec 1, 2018.

  1. Bifflestein

    Bifflestein

    Joined:
    Nov 4, 2017
    Posts:
    7
    Hello, I'm having trouble actually getting an object to move towards my player when they get close to the object. There's no error preventing it from running, it's just that the object doesn't move? Here's my code:
    Code (CSharp):
    1. public class MoveToPlayer : MonoBehaviour {
    2.  
    3.     public float searchRadius = 10f; //distance to start moving
    4.     public float speed = 1f; //speed to move at
    5.     private GameObject Player; //target object
    6.  
    7.     // Use this for initialization
    8.     void Start () {
    9.         Player = GameObject.FindGameObjectWithTag("Player");
    10.     }
    11.  
    12.     // Update is called once per frame
    13.     void Update () {
    14.  
    15.         float dist_ = Vector3.Distance(Player.transform.position, transform.position); //find distance
    16.         if (dist_ < searchRadius)
    17.         {
    18.             Vector3.MoveTowards(transform.position, Player.transform.position, speed * Time.deltaTime); //move towards player
    19.         }
    20.     }
    21. }
    I'm new to C# so if anyone could tell me what I've done wrong, that would be great. Thanks!
     
  2. stefan_s_from_h

    stefan_s_from_h

    Joined:
    Nov 26, 2017
    Posts:
    72
    Vector3.MoveTowards() is only calculating a new Vector3. See the example code for Vector3.MoveTowards.
     
  3. Bifflestein

    Bifflestein

    Joined:
    Nov 4, 2017
    Posts:
    7
    @stefan_s_from_h okay I think I understand. So if moveTowards doesn't actually move the object towards the target and only calculates a new vector, how would I go about actually moving it?

    I made some changes to the code to make it more similar to the example, but still no luck. Sorry, still learning the ropes on C#

    Code (CSharp):
    1. public class MoveToPlayer : MonoBehaviour {
    2.  
    3.     public float searchRadius = 10f;
    4.     public float speed = 1f;
    5.     public Transform target;
    6.  
    7.     // Use this for initialization
    8.     void Start () {
    9.        
    10.     }
    11.    
    12.     // Update is called once per frame
    13.     void Update () {
    14.  
    15.         float dist_ = Vector3.Distance(target.transform.position, transform.position);
    16.         if (dist_ < searchRadius)
    17.         {
    18.             transform.position = Vector3.MoveTowards(transform.position, target.transform.position, speed * Time.deltaTime);
    19.            
    20.         }
    21.     }
    22.  
    23.     private void OnTriggerEnter(Collider other)
    24.     {
    25.         if (other.tag == "Player")
    26.         {
    27.             FindObjectOfType<GameManager>().AddCard(1);
    28.  
    29.             Instantiate(pickupEffect, transform.position, transform.rotation);
    30.  
    31.             Destroy(gameObject);
    32.         }
    33.     }
    34. }
    35.  
     
    Last edited: Dec 1, 2018
  4. stefan_s_from_h

    stefan_s_from_h

    Joined:
    Nov 26, 2017
    Posts:
    72
    Increase speed and searchRadius to see if there's something moving at all. Put some Debug.Log() into the code to see if certain parts of the code are executed.
     
  5. Bifflestein

    Bifflestein

    Joined:
    Nov 4, 2017
    Posts:
    7
    @stefan_s_from_h Increased speed to 10 and searchRadius to 200, but no luck. So I set up a debug message that displays if the object is close to the player, by putting the debug.log in the if statement that checks if dist_ < searchRadius. Turns out that line of code is in fact running, so the distance is working, just not the actual moving of the object. Any other ideas why that might not be working?

    I set the object to be a trigger collider, there's no rigidbody, it's not static. The Transform set up as target is set as the player in the Unity editor. Is there any other info that I can give that might explain why this object just won't move?
     
    Last edited: Dec 2, 2018
  6. stefan_s_from_h

    stefan_s_from_h

    Joined:
    Nov 26, 2017
    Posts:
    72
    I was curious and created a test project with your script. It worked perfectly. The object was moving towards the player.

    Is there another script that manipulates transform.position?

    (By the way: target.transform.position can be written as target.position because the target already is a Transform. target.transform just references itself.)
     
  7. Bifflestein

    Bifflestein

    Joined:
    Nov 4, 2017
    Posts:
    7
    @stefan_s_from_h Hmm weird. there shouldn't be, not with the object in question.Yeah I realized that I could remove the transform part of those lines a little after I sent that updated code.

    There is a line that adjusts transform.rotate, but not position. I commented out the rotation line of code and still nothing happened, so that's not the issue.

    Could it have something to do with the fact that there is a Card Parent object and the code that rotates and moves is in the Card child? I doubt it as it has no issue rotating, but I figure maybe it has something to do with it

    Is there any way I can send you the project file so that you can take a look at it and maybe help me figure out why mine isn't working? Thanks for your help so far!
     
    Last edited: Dec 2, 2018
  8. stefan_s_from_h

    stefan_s_from_h

    Joined:
    Nov 26, 2017
    Posts:
    72
    transform.position is the position in world space. That's independent from the parent of your object. I don't know what you mean with "Card" in this context. Maybe some script in the parent is manipulating the children, too?

    Try testing it by rearranging the hierarchy. This works even during runtime. Take your object and put it somewhere else, not under the Card object. The scripts of the Card object will crash if they rely on the object remaining there forever.

    I'm just a random user. This would be a bit too far. Maybe you find out more and someone else has some ideas.
     
  9. Bifflestein

    Bifflestein

    Joined:
    Nov 4, 2017
    Posts:
    7
    @stefan_s_from_h Figured it out! FINALLY :D haha All I had to do was remove the card object from the card parent (for explanation, my game is a 3d collectathon with cards as one of the collectibles). Now it works perfectly. Still not sure why the parent was preventing the card from moving, but oh well! Thanks for trying to help though!
     
    faizan_raza93 and Gentle-Shark like this.
  10. samf1111

    samf1111

    Joined:
    Sep 18, 2019
    Posts:
    16
    My problem was that i forgot the "transform.position = " part before the Vector3.MoveTowards, for anyone else with problems
     
    StrykerWildfire and shrihan_t like this.
  11. invincibleprogrammer017

    invincibleprogrammer017

    Joined:
    Aug 6, 2020
    Posts:
    1
  12. StrykerWildfire

    StrykerWildfire

    Joined:
    Jul 7, 2020
    Posts:
    1
  13. faizan_raza93

    faizan_raza93

    Joined:
    Dec 21, 2018
    Posts:
    9

    I was also having the same problem .. All I did is that assigned that Vector3.MoveTowards() , to the transform.position
    here is my code this MoveCharacter() is called in Update().
    I need a fix y position so I fixed the y to some value .
    Code (CSharp):
    1. void MoveCharacter()
    2.     {
    3.         Vector3 move = Vector3.MoveTowards(transform.position, player.transform.position, moveSpeed * Time.fixedDeltaTime);
    4.         move.y = 0.91f;
    5.         transform.position = move;
    6.     }