Hello, I am trying to create an enemy with random movement and stop moving (or change direction) when collide with Tilemap collider. I created this script, enemy randomly moving, but still bypassing colliders(water).
public float speed;
private Rigidbody2D rb;
private float currenttime = 0;
public float x;
public float y;
Vector2 randomdir;
public void Start()
{
rb = GetComponent<Rigidbody2D>();
}
public void FixedUpdate()
{
currenttime += Time.fixedDeltaTime;
if(currenttime > 2)
{
x = Random.Range(-1f, 1f);
y = Random.Range(-1f, 1f);
randomdir = new Vector2(x,y);
currenttime = 0;
}
rb.velocity = new Vector2(x, y) * speed;
}