Hello !
So, I’m a beginner with c# and Unity engine. I’m working on a roguelike project without the procedural generation of dungeon, just a map with some spawnpoint. Everything work but I’m unable to make my prefab come after me in a range of 5 squares around them(320 pixels → 1 grid = 64px). The system is turn per turn, and the debug show that the mob move on their turn(in their dream maybe, because they stay at the same place during playmode), had their turn, etc. I added some damage possibility and they effectively damage me without even being near me and the debug confirmed it. So, how ? They don’'t attack when I’m near them on x or y axis but from their position as son as I start an action, which mean at my very first move after starting the playmode.
Here you can see my Enemy Behaviour logic, without the damage logic(I suppress it to put it in another script later):
Here you can see my Enemy Behaviour logic, without the damage logic(I suppress it to put it in another script later):
Here you can see my Enemy Behaviour logic, without the damage logic(I suppress it to put it in another script later):
<>
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyBehavior : MonoBehaviour
{
public float detectionRange = 320f;
public float moveDistance = 64f;
public int health = 100;
public float speed = 2f;
private Transform player;
private void Start()
{
player = GameObject.FindGameObjectWithTag("Player").transform;
}
public void TakeTurn()
{
float distanceToPlayer = Vector2.Distance(transform.position, player.position);
if (distanceToPlayer <= detectionRange)
{
MoveTowardsPlayer();
}
}
private void MoveTowardsPlayer()
{
Vector2 direction = (player.position - transform.position).normalized;
direction = new Vector2(Mathf.Round(direction.x), Mathf.Round(direction.y)) * moveDistance;
Vector2 targetPosition = (Vector2)transform.position + direction;
Collider2D hitCollider = Physics2D.OverlapCircle(targetPosition, 0.1f);
if (hitCollider == null || hitCollider.CompareTag("Player"))
{
transform.position = targetPosition;
}
}
}
<>
And here you can also see the EnemyManager script :
<>
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyManager : MonoBehaviour
{
public static EnemyManager Instance;
void Awake()
{
if (Instance == null)
{
Instance = this;
}
else
{
Destroy(gameObject);
}
}
public void TakeTurn()
{
GameObject[] enemyObjects = GameObject.FindGameObjectsWithTag("enemy");
foreach (GameObject enemyObject in enemyObjects)
{
EnemyBehavior enemy = enemyObject.GetComponent<EnemyBehavior>();
if (enemy != null)
{
enemy.TakeTurn();
}
}
}
public void OnPlayerMoved()
{
TakeTurn();
}
}
<>
I created a enemy tag on my prefabs and they are linked to the behaviour script. The EnemyManager group the list of the enemies and the spawn are working normally, they just don’t make any move… I Just want that when I enter in a 5 cases perimeter around them, they chase after me.
I know about the unity roguelike tutorial but I was trying to make it by myself without just remake another tutorial.