Enemy 2D check for collision
Currently i have a basic enemy follow script but he gets caught on walls and such in my 2D topdown game and i wanted to make him check for collision but im not exactly sure how to do this here is my
public class EnemyFollow : MonoBehaviour {
public float speed;
private Transform target;
private void OnTriggerStay2D(Collider2D collision)
{
{
if (Vector2.Distance(transform.position, target.position) > 1.0)
{
transform.position = Vector2.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
}
}
}
// Use this for initialization
void Start()
{
target = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
}
Answer by xxmariofer · Jan 20, 2019 at 08:26 PM
First, if your object is colliding it will not be called the ontriggerstay2d since you should use oncollisionstay2d, second you should not move your player inside ontriggerstay since its not called every frame and the time.deltatime then would make no sense. (use fixedtime but it wont be the best practice eitther), im having issues to understand your question, if your problem is that when the player enter enemy trigger you want to follow him, but your enemy is colliding, you need pathfinding, whats not an easy thing but if you can giv eit a try here is a blog about it. http://www.jgallant.com/nodal-pathfinding-in-unity-2d-with-a-in-non-grid-based-games/