MoveTowards not causing the object to move

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:

public class MoveToPlayer : MonoBehaviour {

    public float searchRadius = 10f; //distance to start moving
    public float speed = 1f; //speed to move at
    private GameObject Player; //target object

    // Use this for initialization
    void Start () {
        Player = GameObject.FindGameObjectWithTag("Player");
    }
  
    // Update is called once per frame
    void Update () {

        float dist_ = Vector3.Distance(Player.transform.position, transform.position); //find distance
        if (dist_ < searchRadius)
        {
            Vector3.MoveTowards(transform.position, Player.transform.position, speed * Time.deltaTime); //move towards player
        }
    }
}

I’m new to C# so if anyone could tell me what I’ve done wrong, that would be great. Thanks!

Vector3.MoveTowards() is only calculating a new Vector3. See the example code for Vector3.MoveTowards.

3 Likes

@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#

public class MoveToPlayer : MonoBehaviour {

    public float searchRadius = 10f;
    public float speed = 1f;
    public Transform target;

    // Use this for initialization
    void Start () {
       
    }
   
    // Update is called once per frame
    void Update () {

        float dist_ = Vector3.Distance(target.transform.position, transform.position);
        if (dist_ < searchRadius)
        {
            transform.position = Vector3.MoveTowards(transform.position, target.transform.position, speed * Time.deltaTime);
           
        }
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.tag == "Player")
        {
            FindObjectOfType<GameManager>().AddCard(1);

            Instantiate(pickupEffect, transform.position, transform.rotation);

            Destroy(gameObject);
        }
    }
}

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.

@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?

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.)

@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!

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.

@stefan_s_from_h Figured it out! FINALLY :smile: 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!

2 Likes

My problem was that i forgot the "transform.position = " part before the Vector3.MoveTowards, for anyone else with problems

2 Likes

You save my day!

@samf1111 Best.

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 .

void MoveCharacter()
    {
        Vector3 move = Vector3.MoveTowards(transform.position, player.transform.position, moveSpeed * Time.fixedDeltaTime);
        move.y = 0.91f;
        transform.position = move;
    }