I am having trouble making the npc look like he is following the player. So the player walks into the npcs collider box which makes him follow. The problem is when the player is above the enemy the enemy doesn’t change animations to make it look like he is walking up. Same with left right and down. Ive tried and tried and cant figure anything out. Please help me I want the enemy to change sprites when following the player so it actually looks like he is following him. I am not trying to make the enemy rotate I just want new sprites to make it look like he is following in which ever the player is to the enemy.
using UnityEngine;
using System.Collections;
public class SlimeController : MonoBehaviour
{
public Transform Character; // Target Object to follow
public float speed = 0.1F; // Enemy speed
public float maxDist = 10.0f;
public float attackdistance = 3;
public float farenough;
private Vector3 directionOfCharacter;
private bool challenged = false;// If the enemy is Challenged to follow by the player
public Transform StartMarker;
public Transform Target;
private Vector3 goback;
public Rigidbody2D rb;
Animator anim;
bool left;
bool right;
bool up;
bool down;
bool attacking;
float x, y;
private void Start()
{
rb = GetComponent<Rigidbody2D>();
anim= GetComponent<Animator>();
}
void Update()
{
var distanceFromPlayer = Vector3.Distance(Character.position, transform.position);
if (challenged && !attacking)
{
}
if (challenged)
{
directionOfCharacter = Character.transform.position - transform.position;
directionOfCharacter = directionOfCharacter.normalized; // Get Direction to Move Towardsss
transform.Translate(directionOfCharacter * speed, Space.World);
enabled = true;
if (distanceFromPlayer < attackdistance)
{
attack();
}
if (distanceFromPlayer > attackdistance)
{
speed = 0.03f;
}
}
if (!challenged)
{
goback = StartMarker.transform.position - transform.position;
goback = goback.normalized;
transform.Translate(goback * speed, Space.World);
}
}
// Will be triggered as soon as player would touch the Enemy Object
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.name == ("Player"))
{
challenged = true;
}
}
private void OnTriggerExit2D(Collider2D other)
{
if (other.gameObject.name == ("Player"))
{
speed = 0.03f;
challenged = false;
}
}
void attack()
{
speed = 0;
transform.Translate(directionOfCharacter * speed, Space.World);
}
}