Vector2movetowards not working, 2D object staying in place

Vector2movetowards not working. I have been following a tutorial of a 2D platformer game. One person in the comments have the same problem as me.

I have a zombie in my scene that is supposed to move left and right to follow the player. The error is, it doesn’t move from it’s current place, and this only happened after I tried modifying the script by accessing the isGameOver bool from the player manager, before I works perfectly fine. The reason why I tried to access the isGameOver boolean is because there’s also another error, before, whenever the zombie reaches the spot where the player is destroyed(when the player losses) there is a NullReferenceException error. And when I tried to fix that error, the zombie no longer move from its place, and is only playing the chasing animation.

I tried rewriting the zombie scripts three times so I think it is not in the scripts, and I tried isolating the zombie by creating a new project, and it worked there. I also tried removing all of the components attached to the zombie, and even creating a circle sprite tagged as the player to make the zombie move from its place (in the older project), yet it still doesn’t move from its place. What could be conflicting the zombie from moving?

Vector2.MoveTowards certainly does work as it should, especially since the source code is available in the reference repository.

So you’re most likely are using it incorrectly or you have something else interfere with your object (like an Animator / Animation that might mess with your object). Since MoveTowards is a method that has to be used in code, we can’t really help you any further without seeing the code and how you tried to use this method. You mentioned “a tutorial” so it would be great to have that reference as well. However a link to the tutorial does NOT replace the need for you to post your ACTUAL code. We will not sit through a random tutorial if the issue is in the end in your code anyways.

I’m not sure what you mean here. If you just copied someone else’s code without even trying to understand it, you’re wrong here. We’re not writing or fixing code for other peoples here. We’re here to help you so you can fix the issues in your code yourself.

So again, please edit your question and include your actual code that doesn’t work and be more specific about what you expect your code to do and what happens instead. Your current description is just a collection of random abstract statements what you have done to someone else’s code. This won’t lead anywhere.

1 Like

Here is my script for with the movetowards code, I decided to copy the scene from that separate project, created a new animator for the zombie object and copied the script by renaming them, and deleting the older script. Now it works, but I don’t know the reason. Here is my older script.

</>

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ChaseState : StateMachineBehaviour
{
    Transform target;
    public float speed = 3;
    Transform borderCheck;
    // OnStateEnter is called when a transition starts and the state machine starts to evaluate this state
    override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        target = GameObject.FindGameObjectWithTag("Player").transform;
        borderCheck = animator.GetComponent<Zombie>().borderCheck;
    }

    // OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks
    override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        Vector2 newPos = new Vector2(target.position.x, animator.transform.position.y);
        animator.transform.position = Vector2.MoveTowards(animator.transform.position, newPos, speed * Time.deltaTime);
        if (Physics2D.Raycast(borderCheck.position, Vector2.down, 2) == false)
        {
            animator.SetBool("isChasing", false);
        }

        float distance = Vector2.Distance(target.position, animator.transform.position);
        if (distance < 1.5)
        {
            animator.SetBool("isAttacking", true);
        }
    }

    // OnStateExit is called when a transition ends and the state machine finishes evaluating this state
    override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        
    }

    // OnStateMove is called right after Animator.OnAnimatorMove()
    //override public void OnStateMove(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    //{
    //    // Implement code that processes and affects root motion
    //}

    // OnStateIK is called right after Animator.OnAnimatorIK()
    //override public void OnStateIK(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    //{
    //    // Implement code that sets up animation IK (inverse kinematics)
    //}
}

Here are my two other scripts:

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

public class IdleState : StateMachineBehaviour
{
    Transform target;
    Transform borderCheck;

    override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        target = GameObject.FindGameObjectWithTag("Player").transform;
        borderCheck = animator.GetComponent<Zombie>().borderCheck;
    }

    override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {

        if (Physics2D.Raycast(borderCheck.position, Vector2.down, 2) == false)
            return;

        float distance = Vector2.Distance(target.position, animator.transform.position);
        if (distance < 7)
            animator.SetBool("isChasing", true);
    }

    override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        AudioManager.instance.Play("ZombieScream");
    }

    //override public void OnStateMove(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    //{
    //    // Implement code that processes and affects root motion
    //}

    //override public void OnStateIK(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    //{
    //    // Implement code that sets up animation IK (inverse kinematics)
    //}
}

/* Here is my second script*/

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AttackState : StateMachineBehaviour
{
    Transform target;
    override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        target = GameObject.FindGameObjectWithTag("Player").transform;
    }

    override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        float distance = Vector2.Distance(target.position, animator.transform.position);
        if (distance > 2)
            animator.SetBool("isAttacking", false);
    }

    override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        
    }

    //override public void OnStateMove(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    //{
    //    // Implement code that processes and affects root motion
    //}

    //override public void OnStateIK(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    //{
    //    // Implement code that sets up animation IK (inverse kinematics)
    //}
}

I did fix it for now, but I have a feeling it will happen again and I wouldn’t know how to fix it. Can you help me?, I’ll try to replicate the problem to it’s former state and you can help me debug from there. This problem did not occur before I decided to modify the script and tried to access the “isGameOver” bool from another script(player manager". The movetowards was functioning properly, until it didn’t and I tried to redo the script three times. I tried clearing GI cache, and deleting the library thinking it was a data bug but it didn’t work. It only worked when I tried creating a new project, redoing the tutorial to the ChaseState script point. And then copying that scene to my older project where the bug is, and redoing the animator controller, and copying, changing the scripts name, and deleting the older scripts so that there won’t be errors in that project.

I don’t get it, I tried to replicate the old state where the bug occurs but now it works, the bug is gone. The only difference that I noticed is before when the bug occurs, I can see the to arrows(red, green) when I select the zombie and choose the move tool, but now that the bug is gone, when I select the zombie, and move tool, the arrows also does not show up. Only the orange color surrounding the zombie.

There’s another problem. The zombie object works for a while but after a few seconds it stops chasing the player and just plays the idle animation. Before when the bug was occuring the zombie just plays the chasing animation while staying in place.

And also whenever the player dies(destroyed) and the zombie gets to where it is destroyed, there’s a NullReferenceException. This should not happen according to the tutorial, and there’s also one person in the comments experiencing this. The tutorial is titled “2D platformer game” by GDTitans. The bug is on episode 19. Here’s the link:

Can you help me dude.

I’ve done following the tutorial again for the fifth time, and at first it worked, but after some point(I noticed when I disabled the capsule collider in the tutorial) the bug happened again. I tried reverting my steps but the bug is still there. And the Reloading Domain is also taking a long time.

Please help me fix this bug. I’ve been trying to debug it for six days, the movetowards is not showing on the debugging tool, but that line of code is executing, because the debug.log is showing under it. I just can’t fix this bug alone.

I figured it out, it was because I modified the position of the zombie using the animator window. The zombie stays on one position because of that adjustment.