0
\$\begingroup\$

I am developing 2D Top-Down RPG game. I have script that makes my enemy follow the player, and when they touch the player, the player will start to lose HP.

Everything works well, but when they touch each other, the enemy starts pushing the player and also I can push the enemy too.

I use a Rigidbody2D and BoxCollider2d for each of them.

Here is my enemy controller code:

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

public class EnemyController : MonoBehaviour
{
    private Animator myAnim;
    private Transform target;
    public Transform homePos;
    private Rigidbody2D myRB;

    [SerializeField]
    private float speed;
    [SerializeField]
    private float maxRange;
    [SerializeField]
    private float minRange;
    // Start is called before the first frame update
    void Start()
    {
        myAnim = GetComponent<Animator>();
        target = FindObjectOfType<PlayerController>().transform;
        myRB = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Vector3.Distance(target.position, transform.position) <= maxRange && Vector3.Distance(target.position, transform.position)>= minRange)
        {
            FollowPlayer();
        }

        else if(Vector3.Distance(target.position, transform.position) >= maxRange)
        {
            GoHome();
        }
    }

    public void FollowPlayer()
    {
        myAnim.SetBool("isMoving", true);
        myAnim.SetFloat("moveX", (target.position.x - transform.position.x));
        myAnim.SetFloat("moveY", (target.position.y - transform.position.y));
        transform.position = Vector3.MoveTowards(transform.position, target.position, speed * Time.deltaTime);  
    }
    public void  GoHome()
    {
        myAnim.SetFloat("moveX", (homePos.position.x - transform.position.x));
        myAnim.SetFloat("moveY", (homePos.position.y - transform.position.y));
        transform.position = Vector3.MoveTowards(transform.position, homePos.position, speed * Time.deltaTime);

        if (Vector3.Distance(transform.position, homePos.position) == 0)
        {
            myAnim.SetBool("isMoving", false);
        }
    }
}
\$\endgroup\$
2
  • \$\begingroup\$ So what is your expected behaviour that you want to be? It should stick close to the player? Bump him far away? \$\endgroup\$ – Zibelas Mar 10 at 11:55
  • \$\begingroup\$ I have min range and max range values but they still pushing ı want that they not push each other when they touch \$\endgroup\$ – Wallroad Mar 10 at 11:57
0
\$\begingroup\$

I fixed it when i add this codes to my enemy controller when enemy touch to player enemy's speed set to 0 and dont move more

 private void OnCollisionStay2D(Collision2D other)
    {
        if (other.collider.tag == "Player")
        {
            speed = 0f;
        }

    }

    private void OnCollisionExit2D(Collision2D other)
    {
        if (other.collider.tag == "Player")
        {
            speed = respeed;
        }
    }
```
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.