Cant get entities to follow the player using rigidbody?

Hello, currently I am struggling to get entities to follow the player using rigidbody. It works with Transform.moveTowards but this causes the entities to clip through one another and objects. Here is the Code.

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

public class zombieBehavior : MonoBehaviour
{
    public GameObject player;
    private GameObject attack;
    private Rigidbody2D rb;
    public float attackSpeed = 1;
    public float speed = 1;
    public float rSpeed = 1;
    public float proximityOffset;
    public float range;
    public float rotationModifier = 200f;
    private float distance;
    public float health = 100f;
    private SpriteRenderer[] spriteRenderers;




    void Awake()
    {
        rb = GetComponent<Rigidbody2D>();
        spriteRenderers = GetComponentsInChildren<SpriteRenderer>();
        player = GameObject.FindWithTag("Player");

        attack = GameObject.Find("meleeAttack");

        if (spriteRenderers == null)
        {
            Debug.LogError("SpriteRenderer component not found. Please attach a SpriteRenderer to the GameObject.");
        }
    }

    void FixedUpdate()
    {
        distance = Vector2.Distance(transform.position, player.transform.position);
        Vector2 direction = (player.transform.position - transform.position).normalized;

        // Move towards the player
        //transform.position = Vector2.MoveTowards(transform.position, player.transform.position, speed * Time.deltaTime);

        // Rotate to face the player
        Vector3 vectorToTarget = player.transform.position - transform.position;
        float angle = Mathf.Atan2(vectorToTarget.y, vectorToTarget.x) * Mathf.Rad2Deg - rotationModifier;
        Quaternion q = Quaternion.AngleAxis(angle, Vector3.forward);
        transform.rotation = Quaternion.Slerp(transform.rotation, q, Time.deltaTime * rSpeed);


        if (distance > proximityOffset){
            //transform.position = Vector2.MoveTowards(transform.position, player.transform.position, speed * Time.deltaTime);
            Vector2 move = direction * speed;
            rb.velocity = move;



        }else if(distance <= proximityOffset){
            StartCoroutine(Attack());
        }

        // Check if health is zero or less
        if (health <= 0)
        {
            Destroy(gameObject);
        }


    }

    void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.CompareTag("bullet"))
        {
            health -= 25;
            StartCoroutine(FlashRed()); // Start the FlashRed coroutine
        }

        if (collision.gameObject.CompareTag("Zombie"))
        {
            Debug.Log("Collided with Other Zombie");
        }




    }

    private IEnumerator FlashRed()
    {
        if (spriteRenderers != null)
        {
            foreach (var spriteRenderer in spriteRenderers)
            {
                spriteRenderer.color = Color.red;  // Change color to red
            }
            yield return new WaitForSeconds(0.5f);  // Wait for 0.5 seconds
            foreach (var spriteRenderer in spriteRenderers)
            {
                spriteRenderer.color = Color.green;  // Change color back to white
            }
        }
    }

    private IEnumerator Attack()
    {
        attack.SetActive(true);
        yield return new WaitForSeconds(1f);
        attack.SetActive(false);
        yield return new WaitForSeconds(attackSpeed);

    }
}

Rigidbodies are for physics, not for pathfinding. They won't avoid walls for instance, they'll just snag or glitch through.

You want some kind of navigation solution, such as the NavMesh stuff.

There are also 2D navigation solutions, such as this random first google hit:

https://discussions.unity.com/t/682119

1 Like