Hi team,
I know there are similar questions and tried to go with the solution from a couple of them but none worked.
I am trying to move my enemy character towards a random location which I am finding using “Random” method as shown in the script. Now the problem is my character isn’t moving at all and is just hovering over one place only in “Running” animation.
I tried print() method to find if “if-else” is not getting skipped and it is not.
It is like “transform.position” is not working at all:
Script:
public float minX;
public float maxX;
public float minY;
public float maxY;
Vector2 targetPosition;
Animator anim;
public float stopDistance;
private float attackTime;
public float attackSpeed;
public Enemy enemyToSummon;
public float timeBetweenSummons;
private float summonTime;
public override void Start()
{
base.Start();
float randomX = Random.Range(minX, maxX);
float randomY = Random.Range(minY, maxY);
targetPosition = new Vector2(randomX, randomY);
anim = GetComponent<Animator>();
}
private void Update()
{
if (player != null)
{
if ((Vector2)transform.position != targetPosition)
{
anim.SetBool("isRunning", true);
transform.position = Vector2.MoveTowards(transform.position, targetPosition, speed * Time.deltaTime);
}
else
{
anim.SetBool("isRunning", false);
if (Time.time >= summonTime)
{
summonTime = Time.time + timeBetweenSummons;
anim.SetTrigger("summon");
}
}
if (Vector2.Distance(transform.position, player.position) <= stopDistance)
{
if (Time.time >= attackTime)
{
attackTime = Time.time + timeBetweenAttacks;
StartCoroutine(Attack());
}
}
}
}
Script is attached to Summoner empty object which inherits sprites of different parts of enemy.