- Home /
How I make my enemy face the player or flip towards player (top-down game)?
Sorry for my bad English.
2D btw
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Enemy : MonoBehaviour {
// Public Variables
public float speed;
public GameObject effect;
// Private Variables
private Transform playerPos;
private Player player;
private Shake shake;
// Functions
private void Start()
{
shake = GameObject.FindGameObjectWithTag("ScreenShake").GetComponent<Shake>();
player = GameObject.FindGameObjectWithTag("Player").GetComponent<Player>();
playerPos = GameObject.FindGameObjectWithTag("Player").transform;
}
private void Update()
{
transform.position = Vector2.MoveTowards(transform.position, playerPos.position, speed * Time.deltaTime);
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
shake.CamShake();
Instantiate(effect, transform.position, Quaternion.identity);
player.health--;
Destroy(gameObject);
}
if (other.CompareTag("Projectile"))
{
shake.CamShake();
Instantiate(effect, transform.position, Quaternion.identity);
Destroy(gameObject);
}
}
}
Comment
Try to set it y rotation to the exact opposite of the player. For example: The player y rotation is 0, which means it's facing to the right, the enemy will have 0 - 180 = -180 and set that as the rotation of the enemy.
Your answer
Follow this Question
Related Questions
How can I make my Enemy look/flip towards my player (2D TOP-DOWN) 1 Answer
How can I make my enemy always look/flip towards my player (2D top-down)? 1 Answer
EnemyAi animations, are bugged while moving 0 Answers
Unity2D Animation issues 1 Answer
Localscale-flipped 2D character retains original rotation since 5.4 2 Answers