When they are within a very close reach of the player they will do a 180 turn. I don’t know why this happens. I have tried to figure it out, but keep getting the same thing to happen, so I turn to the threads. Here is my enemy script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
public int Health = 100;
public ParticleSystem PS;
public Transform enemypos;
public Rigidbody2D rb;
private bool knockfromright;
public float speed;
private Transform target;
public void Hit(int damage){
Health -= damage;
if(Health <= 0){
die();
}
}
public void die(){
Instantiate(PS, enemypos.position, enemypos.rotation);
Destroy(gameObject);
}
void Start(){
knockfromright = false;
target = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
}
void FixedUpdate()
{
transform.position = Vector2.MoveTowards(transform.position,target.position,speed * Time.fixedDeltaTime);
}
public void LookAt(){
Vector3 directionToTarget = (target.transform.position - transform.position);
transform.rotation = Quaternion.Euler(0,0,Mathf.Atan2(directionToTarget.y - enemypos.position.y,directionToTarget.x - enemypos.position.x) * Mathf.Rad2Deg -90);
}
private void Update()
{
LookAt();
}
}
If anyone can explain what I am doing wrong please do. I have o other scripts that effect how the enemies move.