- Home /
How can I make my enemy always look/flip towards my player (2D top-down)?
MY ENEMY SCRIPT:
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
Answer by xxmariofer · Jan 13, 2019 at 01:09 PM
try adding in update(not tested)
//vector from enemy to the player vector3 _aux = playerPos.position - transform.position;
//creates quaternion Quaternion quat = Quaternion.Euler(_aux); transform.rotation = quaternion;
Your answer
Follow this Question
Related Questions
How can I make my Enemy look/flip towards my player (2D TOP-DOWN) 1 Answer
Flipping enemy 2d character to the side he is walking? 1 Answer
How I make my enemy face the player or flip towards player (top-down game)? 0 Answers
2d enemy prefab (enemy movement and instantiate) 1 Answer
2D Enemy AI X-way only 1 Answer