Why is GameObject2 moving towards the original position of GameObject1 instead of it's current position?

Viewed 15

I currently have one GameObject (an enemy) that should be moving towards another GameObject (the player), but instead moves towards the player's original position of (0, 0). I already had trouble with the enemy moving away from the player instead of towards them, which I fixed by adding a - sign in front of the Vector2. The scripts attached to both objects are below. I know both are very unoptimized in other ways, but for now I'm only concerned with the screwed up movement.

GameObject 1 (player):

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

public class PlayerController : MonoBehaviour
{
    public Rigidbody2D body;
    public float secondFraction = 0.166f;
    public float horizontal;
    public float vertical;
    public float fire;
    public float diaFactor = 0.7f;
    public Vector2 move;
    Transform transform;
    public Collider2D collider2d;
    public float runSpeed = 4f;
    public Bounds Bounds => collider2d.bounds;
    public SpriteRenderer spriteRenderer;
    public bool atkOn = false;
    public bool canAtk = true;
    void Awake()
    {
        Sprite sadBloc = Resources.Load<Sprite>("Sad Pink Bloc");
        Sprite atk1 = Resources.Load<Sprite>("atk1");
        Sprite atk2 = Resources.Load<Sprite>("Attack Pink Bloc-2.png");
        Sprite atk3 = Resources.Load<Sprite>("Attack Pink Bloc-3.png");
        Sprite atk4 = Resources.Load<Sprite>("Attack Pink Bloc-4.png");
        Sprite atk5 = Resources.Load<Sprite>("Attack Pink Bloc-5.png");
        Sprite atk6 = Resources.Load<Sprite>("Attack Pink Bloc-6.png");
        body = GetComponent<Rigidbody2D>();
        collider2d = GetComponent<Collider2D>();
        spriteRenderer = GetComponent<SpriteRenderer>();
        canAtk = true;
        Debug.Log("All good in Awake!");
        Debug.Log(0.166f);
        //spriteRenderer.sprite = Resources.Load<Sprite>("Cyan 128 Bloc");
    }


    IEnumerator startAtk ()
    {
        Sprite happyBloc = Resources.Load<Sprite>("Happy Pink Bloc");
        Sprite atk1 = Resources.Load<Sprite>("atk1");
        Sprite atk2 = Resources.Load<Sprite>("Attack Pink Bloc-2.png");
        Sprite atk3 = Resources.Load<Sprite>("Attack Pink Bloc-3.png");
        Sprite atk4 = Resources.Load<Sprite>("Attack Pink Bloc-4.png");
        Sprite atk5 = Resources.Load<Sprite>("Attack Pink Bloc-5.png");
        Sprite atk6 = Resources.Load<Sprite>("Attack Pink Bloc-6.png");
        Sprite idle = Resources.Load<Sprite>("Pink Bloc");
        atkOn = true;
        canAtk = false;
        Debug.Log("Pre Render");
        spriteRenderer.sprite = atk1;
        yield return new WaitForSeconds(0.166f);
        spriteRenderer.sprite = atk2;
        yield return new WaitForSeconds(0.166f);
        spriteRenderer.sprite = atk3;
        yield return new WaitForSeconds(0.166f);
        spriteRenderer.sprite = atk4;
        yield return new WaitForSeconds(0.166f);
        spriteRenderer.sprite = atk5;
        yield return new WaitForSeconds(0.166f);
        spriteRenderer.sprite = atk6;
        yield return new WaitForSeconds(0.166f);
        spriteRenderer.sprite = idle;
        atkOn = false;
        canAtk = true;
        Debug.Log("Post Render");
    }

    IEnumerator testAtk ()
    {
        Sprite cyanBloc = Resources.Load<Sprite>("Cyan 128 Bloc");
        spriteRenderer.sprite = cyanBloc;
        yield return new WaitForSeconds(1);
        spriteRenderer.sprite = Resources.Load<Sprite>("Pink Bloc");
    }

    protected void Update ()
    {
        horizontal = Input.GetAxis("Horizontal");
        vertical = Input.GetAxis("Vertical");
        fire = Input.GetAxis("Fire1"); 
        move.x = horizontal;
        move.y = vertical;

        if (Input.GetKeyDown("e") && canAtk) 
        {
            Debug.Log("Conditional works!");
            StartCoroutine(startAtk());
        }
    }

    protected void FixedUpdate()
    {  
        if (move.x > 0.01f)
            spriteRenderer.flipX = false;
        else if (move.x < -0.01f)
            spriteRenderer.flipX = true;

        if (move.y > 0.01f)
            spriteRenderer.flipY = false;
        else if (move.y < -0.01f)
            spriteRenderer.flipY = true;
        if (horizontal != 0 && vertical != 0) // Check for diagonal movement
        {
            horizontal *= diaFactor;
            vertical *= diaFactor;
        }
        body.velocity = new Vector2(horizontal * runSpeed, vertical * runSpeed);

    }
}

GameObject 2 (enemy):

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

public class EnemyController : MonoBehaviour
{
    public float zero = 0f;
    public float horizontal;
    public float vertical;
    public float diaFactor = 0.7f;  
    public Rigidbody2D body;
    public Vector2 move;
    public Collider2D collider2d;
    public SpriteRenderer spriteRenderer;
    Transform selfTrans;
    public float runSpeed = 4f;
    public float detectDist = 3.425f;
    public GameObject g;
    Vector2 plrG;
    bool isActive = true;
    bool shouldMove = false;
    Transform plrTrans;
    Sprite emptySprite;
    SpriteRenderer plrSR;
    private PlayerController playerController;

    
    void Awake()
    {
        emptySprite = Resources.Load<Sprite>("nosprite");
        body = GetComponent<Rigidbody2D>();
        plrSR = g.GetComponent<SpriteRenderer>();
        spriteRenderer = GetComponent<SpriteRenderer>();
        selfTrans = GetComponent<Transform>();
        plrTrans = g.GetComponent<Transform>();
        playerController = g.GetComponent<PlayerController>();
    }


    void Update()
    {
        
    }

    void FixedUpdate() 
    {
        plrG = plrTrans.position;

        float step = runSpeed * Time.deltaTime;
        if (isActive)
        {
            Debug.Log("IsActive");
            if (isWithin(detectDist))
            {
                Debug.Log(plrG.x + " " + plrG.y);
                Debug.Log(selfTrans.position.x + " " + selfTrans.position.y);
                body.velocity = -Vector2.MoveTowards(selfTrans.position, new Vector2(plrG.x, plrG.y), step);
                if (isWithin(1f) && playerController.atkOn) 
                {
                    turnOff();
                }
                else if (isWithin(1f))
                {
                    Time.timeScale = 0;
                    plrSR.sprite = Resources.Load<Sprite>("Sad Pink Bloc");
                }
            }
            else 
            {
                body.velocity = new Vector2(0f, 0f);
            }
        }
    }


    void turnOff()
    {
        isActive = false;
        spriteRenderer.sprite = emptySprite;
    }



    bool isWithin(float dist)
    {
        float rDist = Vector2.Distance(selfTrans.position, g.transform.position);
        return rDist < dist;
    }
}```
1 Answers

Here is the problem

body.velocity = -Vector2.MoveTowards(selfTrans.position, new Vector2(plrG.x, plrG.y), step);|

You are setting some position as your velocity. Try starting with something like that:

body.velocity = (plrG - selfTrans.position).normalized * step;
Related