Quantcast
Channel: Questions in topic: "enemy"
Viewing all articles
Browse latest Browse all 1488

How do i make the enemy face the player with this code?,How do I make the enemy face the player?

$
0
0
,I have tried a few ways to do this, but none seemed to work. How do i fix this or what should i do in order to make the enemy face the player? public class Enemy : MonoBehaviour { public float speed; private Transform playerPos; private Player player; public GameObject effect; public int health = 4; void Start() { player = GameObject.FindGameObjectWithTag("Player").GetComponent(); playerPos = GameObject.FindGameObjectWithTag("Player").transform; } void Update() { transform.position = Vector2.MoveTowards(transform.position, playerPos.position, speed * Time.deltaTime); } void OnTriggerEnter2D(Collider2D other) { if (other.CompareTag("Player")) { Instantiate(effect, transform.position, Quaternion.identity); player.health--; Debug.Log(player.health); player.score = player.score - 5; Destroy(gameObject); } if (other.CompareTag("Projectile")) { Instantiate(effect, transform.position, Quaternion.identity); Destroy(gameObject); player.score = player.score + 10; Destroy(other.gameObject); } } void FixedUpdate() { var playerPosition = playerPos.position; Quaternion rot = Quaternion.LookRotation(transform.position - playerPosition, Vector3.forward); transform.rotation = rot; transform.eulerAngles = new Vector3(0, 0, transform.eulerAngles.z); } }

Viewing all articles
Browse latest Browse all 1488

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>